Forum Moderators: coopster

Message Too Old, No Replies

Help getting number of days between 2 dates

         

moroose

8:45 am on May 12, 2008 (gmt 0)

10+ Year Member



Hello
I'd be grateful to anyone who can help me with
returning the number of days by extracting 2 dates

start Date: 2008-05-12 20:32:17
Expiry Date: 2008-08-18 00:17:54

Also,if i convert the above format to a human readable one,can i still get the number of days difference?

I found a snippet code here,but could not successfully apply it to my case
[weberdev.com...]

barns101

9:20 am on May 12, 2008 (gmt 0)

10+ Year Member



One way to do it would be to isolate the first 10 characters (e.g. 2008-05-12) with substr() [php.net] and then use str_to_time() [php.net] to generate two timestamps. You could then work out the number of seconds between the timestamps and therefore the number of days.

Depending on where you're getting the dates from it may be better to use MySQL's timediff() [dev.mysql.com] function.

d40sithui

2:57 pm on May 13, 2008 (gmt 0)

10+ Year Member



try this.

<?
//getting days in between 2 timestamps;
$start = "2008-05-12 20:32:17";
$end = "2008-08-18 00:17:54";

echo "\$start: ".strtotime($start)."<br>";
echo "\$end: ".strtotime($end)."<br>";

$difference = (strtotime($end)-strtotime($start));
echo "\$difference: $difference<br>\n";

$days = $difference/24; //hours in one day
$days = $days/60; //minutes in one hour
$days = $days/60; //seconds in one minute
$days = ceil($days); //rounding up

echo "\$days in between: $days";
?>