Jump to content

Date increment


cvzyl

Recommended Posts

Hi

 

I am a newbie to PHP so if this is a really dumb question, I apologise in advance.

 

I have a value stored in variable $date and want to increment this date by one day, something like:

$date = $date + 1;

 

I cannot find a date increment function in the PHP help file.  I thought that this would be quite a commonly used function but maybe I am looking in the wrong place.

 

How should I go about doing this?

 

Thanks in advance

Cobus

Link to comment
https://forums.phpfreaks.com/topic/41677-date-increment/
Share on other sites

I have a value stored in variable $date

 

How you add 1 day depends on how that date variable is stored

 

If it's in the form of a date, eg

$date = "2007-03-07" ;

$plus_one_day = date ('Y-m-d', strtotime ("+1 days $date"));

 

If it's a unix timestamp in seconds you can add 86400 as above or

$plus_one_day = date ('Y-m-d', strtotime ("+1 days", $date));

 

The advantage of the strtotime method is the increment can be

 

"+x days"

"+x hours"

"-x days"

 

etc etc withour you having to calculate the number of seconds.

 

Link to comment
https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201961
Share on other sites

Thanks for all the comments, I managed to get all the different methods to work but it seems like the suggestion from Barand

$plus_one_day = date ('Y-m-d', strtotime ("+1 days", $date));

is the cleanest solution.

 

My next problem is, how do I "terminate" a date to only contain Y-m-d without any hours or seconds.  I want 2007-03-07 00:00:00 rather than 2007-03-07 23:25:37.  I want to convert a variable (e.g. $date) to this "terminated" value.

 

Cobus

Link to comment
https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201976
Share on other sites

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.