我创建一个排名前30音图..周格式化的日期时间
Hw的我能得到周的两个MySQL格式的日期时间之间有多少?像
来源:2013-01-15 11:41:14
当前日期时间:2013-02-25 13:41:14
我创建一个排名前30音图..周格式化的日期时间
Hw的我能得到周的两个MySQL格式的日期时间之间有多少?像
来源:2013-01-15 11:41:14
当前日期时间:2013-02-25 13:41:14
使用减去次strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference/604800);
您可以使用strtotime()
方法,再算上当前的时间,你在你的MySQL数据库有时间之间的差异。
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
你可以大块下来“剩女”秒和找出分钟(和它的剩余秒),以此类推,直到你打时间你想要的距离。
WEEK()
函数可以帮助您查询吗?
我不知道Mysql的非常好,但让我们假设如下:
select WEEK(date1-date2) from mytable where name='Ken';
使用SQL是更好的,但是在PHP它会是这样的:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
这些更现代的类可能是去的方式:) – 2013-02-26 14:04:13
尝试DATEDIFF (date1,date2)函数在mysql中。 DATEDIFF()会返回两个给定日期之间的日期号。希望你可以很容易地从输出中获得数周。
我如何去除旁边的负号?像'-4' – Ken 2013-02-26 14:02:43
@Ken你可能正在减去小伟大所以减去大 - 小 – 2013-02-26 14:07:03
谢谢你@Mr外国人 – Ken 2013-02-26 14:13:08