Page 1 of 1

Date update code problem.

Posted: Wed Nov 05, 2003 12:42 am
by Kayahk
Basically i am trying to set it up so that it will read the timestamp out of a mysql table, break it down, and then add 1 month to it, ie if the timestamp says its jan 15, 2003 it changes it to feb 15, 2003, and on the end of the months, ie jan 31, if it is larger than the next month it will change it to the last day, ie jan 31 is feb 28(29 when its a leap year)

the problem i am having is that it will work great for the current year, and up updating dec to jan 2004, anything after that reverts it back to 2003

so
Jan 31, 2003 -> Feb 28, 2003
Oct 4, 2003 -> Nove 4, 2003
Dec 31, 2003 -> Jan 31, 2004
Jan 31, 2004 -> Feb 28, 2003(actually it shows up as march 3rd i think?)

here is my code.

Code: Select all

<?php
//TESTING VARIABLES 
//nov 04, 2003 12:30:15 
$date_DATE = "20031104123015"; 
//Extract the date from the mysql format 
$date_year = substr($date_DATE, 0, 4); 
$date_month = substr($date_DATE, 4, 2); 
$date_day = substr($date_DATE, 6, 2); 
$date_hour = substr($date_DATE, 8, 2); 
$date_min = substr($date_DATE, 10, 2); 
$date_second = substr($date_DATE, 12, 2); 

//code to check the date and change it accordingly 
//if date total days in the month is less than the current date, like going from jan to feb use the total days in the next month 

$NMonth = date("t", mktime(1,1,1,($date_month)+1,1,$date_year)); 
//Days in next month.
$CMonthDays = date("d", mktime(1,1,1,$date_month,$date_day,0,$date_year)); 
if ($NMonth < $CMonthDays) 
{ 
$CMonthDays = $NMonth; 
} 
$OldDate = mktime($date_hour, $date_min, $date_second, $date_month, $date_day, $date_year); 
$Updatemydate = mktime($date_hour, $date_min, $date_second, ($date_month)+1, $CMonthDays); 

echo "Start: ".date("Y/m/d H:i:s",$OldDate)."<br>"; 
echo "End: ".date("Y/m/d H:i:s",$Updatemydate)."<br><br>"; 
?>
any help fixing this is appreciated,

thanks.

Posted: Wed Nov 05, 2003 1:06 am
by Kayahk
Wow, *smack head*

simplest thing... was missing $date_year on $Updatemydate.


for some reason i had it in my head that it would be using the servers year instead of the timestamp that was in there.

Posted: Wed Nov 05, 2003 2:08 am
by m3mn0n
Cool. Nice that you found that mistake. Usually when everything is supose to work and your 100% sure you've coded everything right, it's something little like that. ;)