calculating dates

A

Anonymous

Guest
im trying to calcualte a month ahead from a date stored in an sql database and then work out if today is before or after the date calculated, what i have now isnt working cos i dont think im formatting the date right.
any ideas? the date is stored as a datetime


Code:
$today = date("m.d.y"); 
$nextdate = date("m.d.y"); 
$previousdate = $row['Previousdate']; 

$nextdate = date("m", $previousdate)+1; 


if ($nextdate > $today){

cheers for any help
 
Hey cashton2k.

For your first case, there's an easy way with MySQL's INTERVAL keyword and ADDTIME/DATE_ADD functions. This all you can find at the MySQL's Date and Time Functions manual section.

Example of selecting a date/time field value with a month ahead:
Code:
SELECT DATE_ADD(date_field_here, INTERVAL 1 MONTH) FROM table;
I always suggest people to use/store date/time values in Unix Timestamp format. That way you can easily retrieve plus format a date or time value.

Example:
Code:
SELECT DATE_ADD(FROM_UNIXTIME(date_time_field, '%M %D, %Y'), INTERVAL 1 MONTH) FROM table
After you retrieve a given value with your preferred format... you can easily compare it.
Comparing dates... it's easy :) Check out this post.

Hope it helps.
 
Back
Top