Jump to content

arithmetics with dates


eranwein

Recommended Posts

[!--quoteo(post=350130:date=Feb 27 2006, 10:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 10:28 PM) [snapback]350130[/snapback][/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?
Link to comment
Share on other sites

[!--quoteo(post=350130:date=Feb 27 2006, 11:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 11:28 PM) [snapback]350130[/snapback][/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 :)
Link to comment
Share on other sites

[!--quoteo(post=350130:date=Feb 27 2006, 10:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 10:28 PM) [snapback]350130[/snapback][/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:
[code]
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";

[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.