# arithmetics with dates

3 replies to this topic

### #1 eranwein

eranwein
• Members
• Member
• 22 posts

Posted 28 February 2006 - 03:28 AM

i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?

### #2 khburres

khburres
• Members
• Member
• 12 posts

Posted 02 March 2006 - 04:13 PM

[!--quoteo(post=350130:date=Feb 27 2006, 10:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 10:28 PM) [/div][div class=\'quotemain\'][!--quotec--]
i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?
[/quote]

Convert each to their seconds equivalent, subtract the earlier from the latter, then convert number of days, minutes, etc.

How are the dates formatted? Are these MySQL date/times?
He who guards his mouth and his tongue keeps himself from calamity.

### #3 grim1208

grim1208
• Members
• 89 posts

Posted 02 March 2006 - 04:20 PM

[!--quoteo(post=350130:date=Feb 27 2006, 11:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 11:28 PM) [/div][div class=\'quotemain\'][!--quotec--]
i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?
[/quote]
You would want to start off with seperating the date into an array with the explode function and seperate each into month, day, year, hour, minutes, seconds etc.

Also be careful with the dates you will want to look into the gregorian and julian format, this will help set off the different amount of days in the month.

Let me know if you need me to expand in my explanation

edit: google "php explode" or look in the php manual, also google "php GregorianToJD' or look in the php manual. I won't write out the script for you, doing just a little search will definately benifit you. Reply this with what you have come up with or questions and I would be more than happy to assist
grim1208
New Web Projects

### #4 obsidian

obsidian
• Staff Alumni
• 3,202 posts
• LocationSeattle, WA

Posted 02 March 2006 - 04:37 PM

[!--quoteo(post=350130:date=Feb 27 2006, 10:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 10:28 PM) [/div][div class=\'quotemain\'][!--quotec--]
i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?
[/quote]

when you say "formed by date and time," are you referring to a SQL DATETIME (YYYY-MM-DD HH:MM:SS)?

if so, then simply use the strtotime() function on it, and do some arithmetic on the difference between them:
```function getDayDiff(\$date1, \$date2) {
\$diff = abs(strtotime(\$date1) - strtotime(\$date2));
\$day = 60 * 60 * 24; // seconds in a day
return floor(\$diff / \$day);
}

function getHourDiff(\$date1, \$date2) {
\$diff = abs(strtotime(\$date1) - strtotime(\$date2));
\$hour = 60 * 60; // seconds in an hour
return floor(\$diff / \$hour);
}

\$date1 = date('Y-m-d H:i:s'); // right now
\$date2 = '2005-12-25 00:00:00'; // midnight last christmas morning

echo getDayDiff(\$date1, \$date2) . " Days since last Christmas!<br />\n";
echo getHourDiff(\$date1, \$date2) . " Hours since last Christmas!<br />\n";

```

You can't win, you can't lose, you can't break even... you can't even get out of the game.

```<?php
while (count(\$life->getQuestions()) > 0)
{   \$life->study(); } ?>```
LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users