Theory: How to compare dates in PHP.

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
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Theory: How to compare dates in PHP.

Post by seg »

I've noticed a lot of people(in this forum as well as other pleaces)asking about dates/times and how to compare them to current dates/times. I'm here to post the theory behind it, but not the code.

Need: To compare a stored date agaisnt the current date.
First I would suggest saving the dates in a certain format so that they can be delimited easliy later on. For example 10!29!1976. When you need to compare, you can use the explode function to break that down into three variables, 10, 29, 1976. Then it is just a simple matter of comparing... if $today.month >= $saved.month { blah blah blah...

If the date is not saved in a format that is easliy delimited it would just take more code to break it down into variables. A lot of 'if then else' statements would do the trick.

A note about UNIX time.
The time() function returns the number of seconds since January 1 1970 00:00:00 GMT. If you store that number with, say, a news entry, then it is infinatly easier to compare later on when you need to run clean-up scripts. Finding the latest 5 postings would involve a lot less code.

So that's pretty much how it is done. If you are starting a new program, an you know you will need to reference stuff by date try to include the time() function to the list of variables assigned to each group. Some situations, though, might be easier to use the explode method.

After all, in programming, anything is possible, all you need to do is write one more line of code.
-seg

p.s. If someone wants to translate some of all of this into code that would be nice.
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I use this on several of the things I do. Here is 2 examples. This one insert it taken off of some HTML pages I have for PHP examples.

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);
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

That, my friend, is beautiful :D Thank you for posting that. I know it will be a big help to a lot of people.

-seg
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I had to look a lot for something like this when I first decided to do this. Many many example but finally 2 made sence and that is what you see. A combination of 2. I pull the one date out of my Database and use it.

Only problem I have found is its rounding. So where your appointment is 7 days from today at noon it could say 6 or 7 depending on the time of the day you looked at the page. Still trying to find a way to always round down or up.
mindmeddler2002
Forum Newbie
Posts: 21
Joined: Mon Nov 04, 2002 3:09 pm
Location: US, PA, Harrisuburg

Post by mindmeddler2002 »

there is a round function
$rounded = round("3.1", 0); / 3
maybe if u replace the 0 with a -1 it will take a scace away and output 0?

i have a script i use somewhere that takes it down to the secound
btw, that is estimated what u used.

what about if it was a leap year, or if the month was 31 not 30?
btw without a databaese?
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

30 days 31 does not matter. The time difference comes from a date that was inputed into my database at sometime. The code breaks it down into seconds using the unix time feature. So as long as Unix knows months then it works.
mindmeddler2002
Forum Newbie
Posts: 21
Joined: Mon Nov 04, 2002 3:09 pm
Location: US, PA, Harrisuburg

Post by mindmeddler2002 »

yeah
without databases
i added that into my last post...
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Sorry did not understand the part about without a database. Even if you select 2 dates The unix time() will break it apart. As long as they are in the same format that is. ie mm/dd/yyyy.
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

oldtimer wrote:...The unix time() will break it apart...
That's what I forgot to put in my original post, converting an existing d/m/y format to unix time() and then comparing that way.

So.. yeah.. you can do it that way too. :)
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

If you're only comparing dates and time is irrelevant, is there a reason why you have to convert your dates to timestamps? For example, I've got two dates and I'm determining if the current date falls in between them. The following:

Code: Select all

<?php
if (($row['support_start'] <= date("Y-m-d")) && (date("Y-m-d") <= $row['support_end'])) {
     do whatever
}
?>
seems to be doing the trick. Is there a downfall to this method that I'm not seeing up front?
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

I use that method a lot as well. As long as you're storing your dates in a format that you can easily distinguish a greater then, it should be fine. I ALWAYS store my dates as Y-m-d. This way I can sort or compare and it always works great.
Post Reply