PHP两次之间的区别 [英] PHP difference between two times

查看:47
本文介绍了PHP两次之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出两次之间的区别.我只有时间存储在数据库中.我没有存储日期.注意:时间为字符串格式.

I want to find the difference between two times. I have only time stored in database. I don't have date stored. Note : Time is in string format.

情况1:

 Starting time : 06:18:09
 Ending time :  06:18:45

情况2:

Starting time : 06:03:35    
Ending time 06:18:04

案例3:[此案例无效]

Case 3: [This case isn't working ]

Starting time : 23:44:56    
Ending time : 00:45:43

我正在使用此代码在几秒钟内找到,但对我不起作用.

I'm using this code to find in seconds.But not working for me.

$datetime1 = date_create($a);
  $datetime2 = date_create($b);
  $interval = date_diff($datetime1, $datetime2);
 $min=$interval->format('%s');

有什么帮助吗?

谢谢!

推荐答案

第一个冒号的子字符串删除标签,然后使用剩余的时间戳进行解析和减法.

Substring at the first colon to remove the label, then use the remaining time stamp for parsing and subtracting.

使用PHP的 strtotime 方法将时间转换为字符串,然后将两者相减,以秒为单位进行计算.此时,您将有一个号码,然后您就可以使用它进行任何操作.

Use PHP's strtotime method to convert the times as strings into longs, then subtract the two to find the difference in seconds. You'll have a number at this point, which you can then do whatever with.

$datetime1 = substr($datetime1, strpos($datetime1, ":"));
$time1 = strtotime($datetime1);

然后您可以使用 date 方法将时差转换为新的字符串以进行输出.

You can then use the date method to convert the time difference into a new string for outputting.

请参阅:

这篇关于PHP两次之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆