Jump to content

[SOLVED] Question about DATE() .... Hmmmm


bri0987

Recommended Posts

<?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?

Link to comment
https://forums.phpfreaks.com/topic/74600-solved-question-about-date-hmmmm/
Share on other sites

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 :)

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;				  
			  
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.