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
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
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
Share on other sites

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.