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'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败