dongna9185 2016-03-27 16:19
浏览 132

将mm:ss和hh:mm转换为php中的时间戳

How can I convert a string, such as 02:00 to a timestamp that can be saved into the databse as an integer? It can either be mm:ss or hh:mm depending on what I want the user to input in the form. But it's always either mm:ss or hh:mm

strtotime delivers unreliable results, so I figured it's not the right way to go. Is there another way I'm not aware of?

EDIT

To ask the question in a better way: I have a form that has an input for mm:ss and an input for hh:mm values. I need to convert these values to unix timestamps. Obviously strtotime is not the way to go for this.

  • 写回答

1条回答

  • douweng3564 2016-03-27 16:34
    关注

    You cant convert only minutes or hours to timestamp, you need to have a date. In php you can use mktime function:

    $time = mktime($hours, $minutes, $seconds, $month, $day, $year);
    

    Docs: http://php.net/manual/en/function.mktime.php

    For example you have hh:mm input

    $input = '02:15';
    
    $arr = array_map('intval', explode(':', $input));
    
    // Create timestamp with selected time for today date
    $time = mktime($arr[0], $arr[1], 1, date('m'), date('d'), date('Y'));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元