php计算时间差
有个转换问题解决不了$last_time=implode(Db::('table')->field('time')->find()->toArray());//获取到是字符串string类型
结果:2022-11-2 11:47:00 获取是字符串类型,无法与当前时间进行计算差值
date_diff()函数会报错
请问如何计算数据库的$last_time与当前时间差了多少吗?(xx月xx天xx时xx分xx秒) 转换成时间戳呗 https://www.runoob.com/php/func-date-strtotime.html
strtotime
<?php
//数据库时间
$last_time = "2022-11-1 11:47:00";
//当前时间
$ndate = date('Y-m-d H:i:s');
$consdtime = floor((strtotime($ndate) - strtotime($last_time)) / 86400);
echo "时间相差:".$consdtime."天";
?> Takitooru 发表于 2022-11-2 12:22
感谢大佬,strtotime都没百度到{:301_995:} $date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days"); Do_zh 发表于 2022-11-2 13:56
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2) ...
%R%a
这个是啥意思,哪里查到的文档? zpy2 发表于 2022-11-3 06:08
%R%a
这个是啥意思,哪里查到的文档?
php手册是个好东西。
参数 ¶
format
The following characters are recognized in the format parameter string. Each format character must be prefixed by a percent sign (%).
format character Description Example values
% Literal % %
Y Years, numeric, at least 2 digits with leading 0 01, 03
y Years, numeric 1, 3
M Months, numeric, at least 2 digits with leading 0 01, 03, 12
m Months, numeric 1, 3, 12
D Days, numeric, at least 2 digits with leading 0 01, 03, 31
d Days, numeric 1, 3, 31
a Total number of days as a result of a DateTime::diff() or (unknown) otherwise 4, 18, 8123
H Hours, numeric, at least 2 digits with leading 0 01, 03, 23
h Hours, numeric 1, 3, 23
I Minutes, numeric, at least 2 digits with leading 0 01, 03, 59
i Minutes, numeric 1, 3, 59
S Seconds, numeric, at least 2 digits with leading 0 01, 03, 57
s Seconds, numeric 1, 3, 57
F Microseconds, numeric, at least 6 digits with leading 0 007701, 052738, 428291
f Microseconds, numeric 7701, 52738, 428291
R Sign "-" when negative, "+" when positive -, +
r Sign "-" when negative, empty when positive -, Do_zh 发表于 2022-11-3 08:38
php手册是个好东西。
参数 ¶
谢谢分享
页:
[1]