I know date("Y/m/d") is to get today's date like 2008/09/26, and now I want to know the date of 3 days later from date("Y/m/d"). How to do that? How to automatically increase date? For example, after getting date("Y/m/d"), now I want to show the date after it one by one.
$three_days_later = strtotime("+3 days"); //in timestamp // get it in date form $newdate = date("Y/m/d",$three_days_later); //print it out print $newdate; PHP:
Think i'd make use of the mktime() function $threeDaysLater = date("Y/m/d", mktime(0, 0, 0, date("m") , date("d")+3, date("Y")));
in what way? perhaps use a loop. $num_loop = 40; // number of loops :: start from today, print 40 dates for ($looped=0;$looped<$num_loop;$looped++) { print date("Y/m/d" , strtotime("+$looped days") ); // print out the date } PHP: or you want to assign them to an array?
Yes, I want to assign it to an array. Another question, if I now $myDate = "2008/07/25", how to add 3 days to it and then show 2008/08/28?
$num_loop = 9; // number of loops for ($looped=0;$looped<$num_loop;$looped++) { $date_array[$looped] = date("Y/m/d" , strtotime("+$looped days") ); // the array of date start from today & run for $num_loop times } PHP: $date_array will be the array For your another question: you mean add a month also? it will be: $three_days_n_one_month_later = strtotime("+3 days + 1 month"); //timestamp // get it in date form $newdate = date("Y/m/d",$three_days_n_one_month_later); //print it print $newdate; PHP:
No, I mean if I only know $myDate, how do I know 3 days later of $myDate? For example, if I knew $myDate = 2008/07/25, how to add 3 days to $myDate and make it show 2008/07/28.
$num_loop = 9; // number of loops for ($looped=0;$looped<$num_loop;$looped++) { $date_array[$looped] = date("Y/m/d" , strtotime("+$looped days") ); // the array of date start from today & run for $num_loop times } PHP: $date_array will be the array $date_array[3] will be 3 days later
Since date("Y/m/d") will get the date of today, not a specified date (maybe a date in past or in future). For example I will record a date as $myDate, and I want to add 3 days to $myDate to get the date of 3 days later after $myDate. How to do that?
just add the date into the strtotime() the like below: $date_later = strtotime("2008/10/21 +3 days + 1 month"); //in timestamp // get it in date form $newdate = date("Y/m/d",$date_later); PHP: or $originaldate = '2008/10/21'; $date_later = strtotime("$originaldate +3 days + 1 month"); //in timestamp // get it in date form $newdate = date("Y/m/d",$date_later); PHP: and finally the array form you want: $num_loop = 9; // number of loops $originaldate = '2008/9/27'; for ($looped=0;$looped<$num_loop;$looped++) { $date_array[$looped] = date("Y/m/d" , strtotime("$originaldate +$looped days") ); } PHP: the array above is limited to num of loops this function below can do for any date (controllable future or past), maybe its a long step =.= function getthedate($now,$changein_day,$pom_day,$changein_month,$pom_month) { // $now in the form : 2008/8/17 /* the script below doesn't work =.= why? if ($changein_day >= 0) {$day = '+'.$changein_day;} elseif ($changein_day < 0) { $day = '-'.$changein_day; } if ($changein_month >= 0) {$month = '+'.$changein_month;} elseif ($changein_month < 0) { $month = '-'.$changein_month; } $changed_ts = strtotime("$now $day days $month months"); //in timestamp $newdate = date("Y/m/d",$changed_ts); */ if ($pom_day == '-') { $pom_day = '-'; } else { $pom_day= '+';} if ($pom_month == '-') { $pom_month = '-'; } else { $pom_month= '+';} $changed_ts = strtotime("$now $pom_day $changein_day days $pom_month $changein_month months"); //in timestamp $newdate = date("Y/m/d",$changed_ts); return $newdate; } print getthedate('2008/5/5',10,'+',1,'-'); // + 10 days - 1 month // getthedate('2008/5/5',1,'-',10,'-'); // - 1 day - 10 month // getthedate('2008/5/5',5,'+',50,'+'); // + 5 day + 50 month PHP:
Another way to do this is by knowing that strtotime() accepts a 2nd argument used as the offset timestamp. The default is of course time() (meaning now) so if you passed strtotime($myDate) as the 2nd argument then it would do the day increase to that one. strtotime("+3 days", strtotime($myDate)) would add 3 days to the $myDate timestamp. Just seems a bit cleaner to me than strtotime("$myDate +3 days") for some reason. Not sure of its efficiency though since it's calling that function twice.
Thanks all, I tried $incDate=strtotime("$startDate + 3 days"); plus date("Y/m/d", $incDate); and it's working.