Theory: How to compare dates in PHP.
Moderator: General Moderators
Theory: How to compare dates in PHP.
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.
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.
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);
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);
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.
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
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?
$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?
-
mindmeddler2002
- Forum Newbie
- Posts: 21
- Joined: Mon Nov 04, 2002 3:09 pm
- Location: US, PA, Harrisuburg
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:
seems to be doing the trick. Is there a downfall to this method that I'm not seeing up front?
Code: Select all
<?php
if (($row['support_start'] <= date("Y-m-d")) && (date("Y-m-d") <= $row['support_end'])) {
do whatever
}
?>