Php get how many days and hours left from a date
Asked Answered
P

6

14

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. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this

Prolong answered 19/9, 2011 at 17:19 Comment(0)
M
30

PHP fragment:

<?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.

Matronage answered 19/9, 2011 at 17:28 Comment(3)
this one should answer the question asked. thanks, helped me as wellEmbodiment
So is there possibility of having minutes left or not ??Sportswear
@Sportswear yes. Switch the $hours to use floor instead of round, subtract the $hours off the $diff (like with $days to find $hours), divide the resulting amount by 60 and round (to get minutes) /*...*/$secondsPerHour=60*60; $secondsPerDay=60*60*24; $days=floor($diff/$secondsPerDay); $diff-=$days*$secondsPerDay; $hours=floor($diff/$secondsPerHour); $diff-=$hours*$secondsPerHour; $minutes=round($diff/60);Matronage
S
22

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";
Skirr answered 19/9, 2011 at 17:22 Comment(0)
B
21

as of PHP 5.3.0 you could use build-in Date object:

$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

Brusa answered 19/9, 2011 at 17:37 Comment(1)
I was just wondering what params I can use. Here's the answer: php.net/manual/en/…Wilfredowilfrid
S
1

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";
Stahl answered 19/9, 2011 at 17:24 Comment(0)
S
1

The easiest way is improved answer from CountZero. I used this solution for counter for time remaining before expiration of offer. Addition to first three lines of CountZero code:

$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.

Statesman answered 26/5, 2015 at 19:55 Comment(0)
M
0

You can find current date and time using date() function and then subtract

$x = 2011-09-23 - current_date

this will give you no. of days left.

Do same with time as well ..

Hope this helps

Mcgann answered 19/9, 2011 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.