calculating date differences

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

calculating date differences

Post by mzfp2 »

Hi

anybody help me calculate number of days in between two dates?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the number of seconds per day is 84600.
Convert your dates to a unix timeformat (seconds since epoch) and subtract them ;)
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Wish I could paste this in HTML so you could see the colors without me spending alot of time converting. However if you would like to see it go to my site here. http://www.coastgames.com/html.php?link=11

Time difference. Days past. Timestamp in format of mm/dd/yyyy


$day=substr($timestamp, 3, 2);
$month=substr($timestamp, 0, 2);
$year=substr($timestamp, 6, 4);
// Calculations
$startday = mktime ("", "", "", $month, $day, $year); // get unix time for start date
$today = time(); // todays
$difference = $today - $startday; // work out the difference
$show = floor($difference / 86400);

Time Difference. Days to come. Timestamp in format of mm/dd/yyyy

$day=substr($timestamp, 3, 2);
$month=substr($timestamp, 0, 2);
$year=substr($timestamp, 6, 4);
// Calculations
$startday = mktime ("", "", "", $month, $day, $year); // get unix time for start date
$today = time(); // todays
$difference = $startday - $today; // work out the difference
$show = floor($difference / 86400);


--------------------------------------------------------------------------------

Lets break it down. Timestamp example 10/31/2002

$month=substr($timestamp, 0, 2); (This is taking the first 2 digits for the month.)
$day=substr($timestamp, 3, 2); (This is taking the 4th and 5th digit for the day.)
$year=substr($timestamp, 6, 4); (This is takign the 7th through 10th digits for the year.)
// Calculations
$startday = mktime ("", "", "", $month, $day, $year); // get unix time for start date
$today = time(); // todays
$difference = $startday - $today; // work out the difference
$show = floor($difference / 86400);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you want your PHP code to be highlighted use the [syntax=php]...[/syntax] bbcode tags:
faq.php?mode=bbcode

Mac
mester
Forum Newbie
Posts: 5
Joined: Fri Aug 02, 2002 3:40 am

1070

Post by mester »

What about dates prior 1970, the Unix 'BigBang' ?
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Code: Select all

<?php
echo "Thanks<BR>I never even noticed that Tag before.";
?>

Posted: Mon Nov 25, 2002 1:34 am Post subject:

--------------------------------------------------------------------------------

If you want your PHP code to be highlighted use the

Code: Select all

...
bbcode tags:
faq.php?mode=bbcode

Mac
?>
Post Reply