Jump to content

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


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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.