Show correct datediff

Codes here !

Moderators: macek, egami, gesf

Post Reply
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Show correct datediff

Post by Nullsig » Wed May 30, 2012 11:41 am

If you are asking about comparing two dates in PHP, you can do the following:

Code: Select all

<?php
$startDate = strtotime("2012-01-01 15:38:45");//arbitrary time I have chosen for the example
$endDate = strtotime("2015-05-02 05:18:42");//arbitrary time I have chosen

$dateDiffInSeconds = $endDate - $startDate; //number of seconds that occur between the two. 

//calculate years between not necessary for your request but lets make this scalable.
$numYears = floor($dateDiffInSeconds/(60*60*24*365));//total whole years between two dates
$remainder = $dateDiffInSeconds % (60*60*24*365);//number of seconds left after full years are removed

//calculate days from the remainder
$numDays = floor($remainder /(60*60*24));//total whole days, after years are removed, between two dates
$remainder = $remainder % (60*60*24);//number of seconds left after full days are removed

//calculate hoursfrom the remainder
$numHours = floor($remainder /(60*60));//total whole hours, after years & days are removed
$remainder = $remainder % (60*60);//number of seconds left after full years & days are removed

//calculate minutes from the remainder
$numMinutes = floor($remainder /60);
$remainder = $remainder % 60;

$numSeconds = $remainder;
?>

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Show correct datediff

Post by Nullsig » Mon Jun 04, 2012 4:23 am

$startDate = strtotime("now");

Emy
New php-forum User
New php-forum User
Posts: 16
Joined: Sat Jun 02, 2012 8:16 pm

Re: Show correct datediff

Post by Emy » Mon Jun 04, 2012 10:03 pm

Best thing is to convert the date into timestamp and do the difference, then display it as : date();

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests