bri0987 Posted October 24, 2007 Share Posted October 24, 2007 <?php $today = date("F j, Y"); echo $today ?> // this code will Echo todays date: October 24, 2007 I need to ADD 10 days to that. >> So if the date is October 24, 2007 I need it to echo November 3, 2007 << What is the simplest way to do this. I have tried simple math on the string but it did not work? Any one know about this? Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/ Share on other sites More sharing options...
pocobueno1388 Posted October 24, 2007 Share Posted October 24, 2007 This should do it. <?php $new_date = date ('F j, Y', strtotime ("$date + 10 days")); echo $new_date; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377039 Share on other sites More sharing options...
Wuhtzu Posted October 24, 2007 Share Posted October 24, 2007 Make use of the mktime() function http://dk2.php.net/mktime <?php //Save the current time to make sure all calculations are based on the same timestamp //Worst case scenario is that a calc starts 0.001s before midnight and ends 0.001s after midnight $time = time(); //Days to add $days_to_add = 10; //Get the day, month and year of today $today_day = date("j", $time); $today_month = date("n",$time); $today_year = date("Y", $time); //Create the new timestamp $new_timestamp = mktime(0, 0, 0, $today_month, $today_day + $days_to_add, $today_year); //View the todays date and the new date echo "Today: " . date("F j, Y", $time); echo "<br>"; echo "New date: " . date("F j, Y", $new_timestamp); ?> Code tested and working The smart thing about mktime() is that it "corrects for wrong dates" in a very useful manner. $today_day + $days_to_add could easily equal something above 31 (like now when you add 10 to 24 which is 34) but mktime() can handle to make a timestamp for the 34th of October which is actually the 3rd of November... it simply corrects both date, month and year. So this method is very very good for adding x hours, minutes, seconds, days, months or years to a timestamp since mktime() does all the hard work for you Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377043 Share on other sites More sharing options...
Wuhtzu Posted October 24, 2007 Share Posted October 24, 2007 pocobueno1388, now my answer seems pretty embarrassing Is your way the smartest and always working way to add hours, minutes, seconds, days, months and years to a given timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377045 Share on other sites More sharing options...
kenrbnsn Posted October 24, 2007 Share Posted October 24, 2007 Adding days in the strtotime() function works fine and, IMHO, is easier to use than mktime(). Ken Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377046 Share on other sites More sharing options...
Wuhtzu Posted October 24, 2007 Share Posted October 24, 2007 It seems like a great function... can you add 10seconds, 80hours, 2months and 10years at the same time or will you then have to call the function for each addition? **Edit** Sorry for asking - I forgot all about the manual. Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377048 Share on other sites More sharing options...
bri0987 Posted October 24, 2007 Author Share Posted October 24, 2007 THANKS ALL!!! I used this: <?php //date today $date = date("F j, Y"); //put into string $current_date = strtotime($date); //add 10 days $new_date = date("F j, Y",$current_date+=864000); //display dates echo 'Todays date is ' . $date . '<br />'; echo 'Plus 10 days is ' . $new_date; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377049 Share on other sites More sharing options...
kenrbnsn Posted October 24, 2007 Share Posted October 24, 2007 You can also do: <?php <?php $today = date('F j, Y'); $today_plus_ten = date('F j, Y',strtotime('today +10 days')); echo $today . ' ... ' . $today_plus_ten . "<br>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/#findComment-377054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.