简体   繁体   English

Php从日期开始剩下多少天和几小时

[英]Php get how many days and hours left from a date

I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left until the date is reached. 我有一个created_at日期保存喜欢这个“2011-09-23 19:10:18”而且我希望得到日​​期和时间,直到达到日期。 How do I do that? 我怎么做? and column name in database remain days automatically update daily with remain days, please solve this 和数据库中的列名保持天数每天自动更新并保留天数,请解决此问题

PHP fragment: PHP片段:

<?php

//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)

//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));

//Report
echo "$days days $hours hours remain<br />";
?>

Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate. 注意小时和没有分钟/秒的考虑意味着它可能稍微不准确。

This should seed your endeavor. 这应该是你努力的结果。

getdate(strtotime("2011-09-23 19:10:18"))

Full conversion: 完全转换:

$seconds = strtotime("2011-09-23 19:10:18") - time();

$days = floor($seconds / 86400);
$seconds %= 86400;

$hours = floor($seconds / 3600);
$seconds %= 3600;

$minutes = floor($seconds / 60);
$seconds %= 60;


echo "$days days and $hours hours and $minutes minutes and $seconds seconds";

as of PHP 5.3.0 you could use build-in Date object: 从PHP 5.3.0开始,您可以使用内置Date对象:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

http://www.php.net/manual/en/book.datetime.php http://www.php.net/manual/en/book.datetime.php

The easiest way is improved answer from CountZero. 最简单的方法是改进CountZero的答案。 I used this solution for counter for time remaining before expiration of offer. 我使用此解决方案作为计数器在要约到期之前剩余的时间。 Addition to first three lines of CountZero code: 除了前三行CountZero代码:

$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;

And now, you can use string functions to moderate your return values, to merge all or add '0' or '00' in front of the values. 现在,您可以使用字符串函数来调整返回值,合并所有值或在值前面添加“0”或“00”。

it would be something like 它会是这样的

echo $date = date("Y-m-d H:i:s");echo "\n";

$original=time($date);


$modified = "2011-09-23 19:10:18";

echo date("Y-m-d H:i:s",$modified);echo "\n";

You can find current date and time using date() function and then subtract 您可以使用date()函数找到当前日期和时间,然后减去

$x = 2011-09-23 - current_date $ x = 2011-09-23 - current_date

this will give you no. 这会给你不。 of days left. 剩下的日子。

Do same with time as well .. 也和时间一样......

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM