Compare Two Dates
Moderator: General Moderators
Compare Two Dates
I have two dates that I want to compare how many days away there is between each other.
The two date variables are $ecd and $current_date.
The $ecd is a stored date and the $current_date variable is today's date. I want the program to print out how many days it is away the $ecd is from the $current_date.
Thank you!
The two date variables are $ecd and $current_date.
The $ecd is a stored date and the $current_date variable is today's date. I want the program to print out how many days it is away the $ecd is from the $current_date.
Thank you!
- chrys
- Forum Contributor
- Posts: 118
- Joined: Tue Oct 04, 2005 9:41 am
- Location: West Roxbury, MA (Boston)
Here's a ramshackle way to do it:
Code: Select all
$date1 = strtotime($current_date);
$date2 = strtotime($ecd);
$diff = $date1 - $date2;
// $diff is the difference in seconds between those two dates, so...
$days = $diff/60/24;- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
chrys did it for you in the last step.
X number of seconds divide by 60 seconds in a minute divide by 60 minutes in an hours divide by 24 hours in a day.
X number of seconds divide by 60 seconds in a minute divide by 60 minutes in an hours divide by 24 hours in a day.
Code: Select all
<?php
// One day equals 86400 seconds
$days = $diff/86400;
?>Oh Yeah...
I see, don't mind me I'm slow
Thanks chris!
Thanks chris!
Not Working
Here is what I get from this code.
Print Out: -20220
Help anyone?
Print Out: -20220
Code: Select all
$currentdate = date("m-d-Y");
$date1 = strtotime($currentdate);
$date2 = strtotime($row["ecd"]);
$diff = $date1 - $date2;
$days = $diff/60/24;
echo $days . "<br>";- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Try changing your first and second date based on which is bigger. This way you will always have a positive number...
Code: Select all
<?php
$currentdate = date("m-d-Y");
$date1 = strtotime($currentdate);
$date2 = strtotime($row["ecd"]);
if ($date1 > $date2)
{
$diff = $date1 - $date2;
}
elseif ($date2 > $date1)
{
$diff = $date2 - $date1;
}
else
{
$diff = 0;
}
$days = $diff/86400;
$days = number_format($days, 0, '', '');
echo $days . "<br>";
?>Still Not Working
Using the exact same code above I get the print out below:
120
That prints out if the $row["ecd"]; date equals 02-11-2006.
Still not working, I'm curious to see if you have to multiply at all?
120
That prints out if the $row["ecd"]; date equals 02-11-2006.
Still not working, I'm curious to see if you have to multiply at all?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Change the upper code a pinch.
FROM THIS...
TO THIS...
Also, what format is the $row["ecd"] value?
FROM THIS...
Code: Select all
<?php
$currentdate = date("m-d-Y");
$date1 = strtotime($currentdate);
$date2 = strtotime($row["ecd"]);
?>Code: Select all
<?php
$date1 = time();
$date2 = strtotime($row["ecd"]);
?>ECD Format
$row["ecd"] is in date("m-d-Y"); that format.
That is how it is stored.
That is how it is stored.
strtotime() takes a worded date, such as "Wed Feb 15, 2006 6:50 pm" and converts it to a timestamp (the number of seconds since the Epoch)
Read more about this by clicking these functions: time(), date(), strtotime()
Read more about this by clicking these functions: time(), date(), strtotime()
Last edited by m3mn0n on Wed Feb 15, 2006 7:02 pm, edited 1 time in total.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
So in the database, your data is stored as "m-d-Y". What is the MySQL Column data type? Have you run your query in a MySQL query browser (phpMyAdmin, Navicat, SQLYog) to see what the resulting data is from your query? Just a suggestion.
What should happen is that throwing the date value through strtotime should turn it into a unix_timestamp value as long as the date you are running through the function is 1-1-1970 and later. Then if the db date timestamp is greater than today's timestamp, today's timestamp (seconds) should be subtracted from the db date timestamp (also seconds). What is left is the difference in seconds between today and the db date. To convert it to days you need to divide the number of seconds by the number of seconds in a minute. Then divide the numbers of minutes by the number of minutes in an hour. Then divide that by the number of hours in a day. That should leave you with the difference of days. This all hinges upon your server time and the time value returned by your date data.
What should happen is that throwing the date value through strtotime should turn it into a unix_timestamp value as long as the date you are running through the function is 1-1-1970 and later. Then if the db date timestamp is greater than today's timestamp, today's timestamp (seconds) should be subtracted from the db date timestamp (also seconds). What is left is the difference in seconds between today and the db date. To convert it to days you need to divide the number of seconds by the number of seconds in a minute. Then divide the numbers of minutes by the number of minutes in an hour. Then divide that by the number of hours in a day. That should leave you with the difference of days. This all hinges upon your server time and the time value returned by your date data.
HELP!!!
The script below is my current script for comparing two dates (basically what I am trying to do). I want to rewrite this script. I am wondering if storing the dates in any other format other than ("m-d-Y") would make this process any easier? Right now the field type is just text, I was also wondering if changing the field type to date would make it easier?
Suggestions welcome!!!!
$diff - gives me the numerical value of the difference in days.
$date1 - just the numerical day of today.
$date2 - just the numerical value of the stored ECD.
PROBLEM: If the month is different like
$date1 is Januarary 1
$date2 is February 22
this program doesent work cause its simply subtracting two numbers, its not going by months, or how many days are in the month for that matter.
Here is the snippet out of my program:
Suggestions welcome!!!!
$diff - gives me the numerical value of the difference in days.
$date1 - just the numerical day of today.
$date2 - just the numerical value of the stored ECD.
PROBLEM: If the month is different like
$date1 is Januarary 1
$date2 is February 22
this program doesent work cause its simply subtracting two numbers, its not going by months, or how many days are in the month for that matter.
Here is the snippet out of my program:
Code: Select all
<?php
$date1 = strftime("%d");
list($month, $day) = explode("-", $row["ecd"]);
$date2 = $day;
if($date1 > $date2) {
$diff = $date1 - $date2;
} elseif ($date2 > $date1) {
$diff = $date2 - $date1;
} else {
$diff = "0";
}
if($diff >= "7" || $diff <= "0" || $row["note"] == "false") {
if($row["assigned"] == "false") {
$currenttime = time();
$futurdate = strtotime("+10 days", $currenttime);
echo date("m-d", $futurdate);
} elseif($row["assigned"] == "true" && $row["sent_to_reviewer"] == "false") {
$currenttime = time();
$futurdate = strtotime("+7 days", $currenttime);
echo date("m-d", $futurdate);
} elseif($row["sent_to_reviewer"] == "true") {
$currenttime = time();
$futurdate = strtotime("+2 days", $currenttime);
echo date("m-d", $futurdate);
}
} else {
list($month, $day) = explode("-", $row["ecd"]);
$new_date = $month . "-" . $day;
echo $new_date;
}
?>