在文本时间中添加24小时,并使用PHP计算剩余时间
好时光在文本时间中添加24小时,并使用PHP计算剩余时间,php,Php,好时光 我有一个类似于2017-05-11 22:35:45的文本时间,我想在这个时间上加上24小时并计算剩余时间。 E.x: 第一次我有时间:2017-05-11 21:00:00 第二,我想在这个时间上加上24小时:2017-05-12 21:00:00 第三,我想计算从当前时间到2017-05-12 21:00:00的剩余时间(例如,当前时间是2017-05-11 22:10:00):22小时50分钟 那么如何使用PHP实现呢?我不会直接发布工作代码,因为看起来您需要了解更多有关PHP函数
我有一个类似于
2017-05-11 22:35:45的文本时间,我想在这个时间上加上24小时并计算剩余时间。
E.x:
第一次我有时间:2017-05-11 21:00:00
第二,我想在这个时间上加上24小时:2017-05-12 21:00:00
第三,我想计算从当前时间到2017-05-12 21:00:00的剩余时间(例如,当前时间是2017-05-11 22:10:00):22小时50分钟
那么如何使用PHP实现呢?我不会直接发布工作代码,因为看起来您需要了解更多有关PHP函数的信息。干得好。这三个功能将完成这项工作。
首先,您需要将字符串转换为时间戳进行计算,然后您可以计算所需的所有内容。公平的比较永远不要忘记。使用&
使用
演示:
希望这有帮助。
<?php
$starttime = strtotime('2017-05-11 21:00:00');
$starttimeformat = date('Y-m-d H:i:s', $starttime);
echo "Current Time:";
echo $starttimeformat;
echo '<br/>';
echo '<br/>';
$onedayadedtime_format = date('Y-m-d H:i:s', strtotime('+24 hours', $starttime));
echo "End Time after adding 24 hours:";
echo $onedayadedtime_format;
echo '<br/>';
echo '<br/>';
$currenttime = time();
$currenttimeformat = date('Y-m-d h:i:s', $currenttime);
echo "Current Time:";
echo $currenttimeformat;
echo '<br/>';
echo '<br/>';
$onedayadedtime_formatobject = date_create($onedayadedtime_format);
$currenttimeformatobject = date_create($currenttimeformat);
$datedifference = date_diff($onedayadedtime_formatobject , $currenttimeformatobject);
echo "Time difference between dates ";
echo $onedayadedtime_format.' and '.$currenttimeformat;
echo '<br/>';
echo '<br/>';
echo "Hours: ";
echo $datedifference->h;
echo '<br/>';
echo "Minutes: ";
echo $datedifference->i;
echo '<br/>';
echo "Seconds: ";
echo $datedifference->s;
echo '<br/>';
echo "Year: ";
echo $datedifference->y;
echo '<br/>';
echo "Month: ";
echo $datedifference->m;
echo '<br/>';
echo "Days: ";
echo $datedifference->d;
echo '<br/>';
?>
您应该先试试。。。当你陷入困境时,问一下。@HossamMagdy,我想你不知道;)我试过了,但什么也找不到。你应该把你试过的代码贴出来。我们不知道谁投了票,不管是赞成票还是反对票。我们不能&我们不应该。$剩余的
可能是$now=new DateTime()
谢谢@alistaircol!这很有帮助谢谢你@HossamMagdy!这很有帮助,但我不能给你的帖子加分,因为我需要更多的分数。谢谢,没有问题@Amir。很高兴知道这很有用:)
<?php
$input = '2017-05-11 21:00:00';
$plus_24hrs = DateTime::createFromFormat('Y-m-d H:i:s', $input)->modify('+24 hour');
echo '+24hrs = ' . $plus_24hrs->format('Y-m-d H:i:s') . PHP_EOL;
$remaining = DateTime::createFromFormat('U', time());
$diff = $remaining->diff($plus_24hrs);
echo 'to go: ' . $diff->format('%hh %im %ss');
+24hrs = 2017-05-12 21:00:00
to go: 12h 24m 32s
<?php
$starttime = strtotime('2017-05-11 21:00:00');
$starttimeformat = date('Y-m-d H:i:s', $starttime);
echo "Current Time:";
echo $starttimeformat;
echo '<br/>';
echo '<br/>';
$onedayadedtime_format = date('Y-m-d H:i:s', strtotime('+24 hours', $starttime));
echo "End Time after adding 24 hours:";
echo $onedayadedtime_format;
echo '<br/>';
echo '<br/>';
$currenttime = time();
$currenttimeformat = date('Y-m-d h:i:s', $currenttime);
echo "Current Time:";
echo $currenttimeformat;
echo '<br/>';
echo '<br/>';
$onedayadedtime_formatobject = date_create($onedayadedtime_format);
$currenttimeformatobject = date_create($currenttimeformat);
$datedifference = date_diff($onedayadedtime_formatobject , $currenttimeformatobject);
echo "Time difference between dates ";
echo $onedayadedtime_format.' and '.$currenttimeformat;
echo '<br/>';
echo '<br/>';
echo "Hours: ";
echo $datedifference->h;
echo '<br/>';
echo "Minutes: ";
echo $datedifference->i;
echo '<br/>';
echo "Seconds: ";
echo $datedifference->s;
echo '<br/>';
echo "Year: ";
echo $datedifference->y;
echo '<br/>';
echo "Month: ";
echo $datedifference->m;
echo '<br/>';
echo "Days: ";
echo $datedifference->d;
echo '<br/>';
?>