Show correct datediff

Codes here !

Moderators: macek, egami, gesf

Bulgarin
New php-forum User
New php-forum User
Posts: 4
Joined: Wed May 30, 2012 8:38 am

Show correct datediff

Postby Bulgarin » Wed May 30, 2012 11:15 am

Hello! I'm new in php-word and have one question.
Can you tell me how to get datediff like "34 days, 7 hours, 22 minutes" (for example) , when I compare Date from Database nad current date (now() ) ? Thank you!

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

Postby 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;
?>

Bulgarin
New php-forum User
New php-forum User
Posts: 4
Joined: Wed May 30, 2012 8:38 am

Re: Show correct datediff

Postby Bulgarin » Wed May 30, 2012 12:21 pm

Thank you, I'll try it!

Bulgarin
New php-forum User
New php-forum User
Posts: 4
Joined: Wed May 30, 2012 8:38 am

Re: Show correct datediff

Postby Bulgarin » Sat Jun 02, 2012 3:42 am

How in $startDate put the currend date and time?

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

Postby 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

Postby 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();


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 3 guests

cron