Jump to content

[SOLVED] Manipulating dates


Johnain

Recommended Posts

Hi all

 

I am constantly baffled by the unbelievable difficulty of carrying out simple date manipulations in PHP. I have just spent around six hours trying to add a day to a date and I still do not have the answer.

 

I have a date stored in a date filed in my MySQL database in the format YYYY-DD-MM. All I want to do is retrieve it, add a day to it and display it in an HTML form as DD-MM-YYYY. I already have a function that will reverse it to DD-MM-YYYY for display and another that will reverse it back to my YYYY-MM-DD format when I wish to write it back to the database, so it's just adding a day (or two days, or any other number of days) that I need to do.

 

But the apparently simple task of adding one (or more) days to it has proved beyond me.

 

I have scoured the web and seen almost every solution known to mankind. Most of them incredibly complex.

 

It must be me being an idiot. Can anybody help, please.

 

My latest attemp is this ...

 

function calcdate($indate, $noofdays)  {

// calculates a number of days from the supplied date, plus (no sign needed) or minus (give - sign in $noofdays

// Assumes that indate is yyyy-mm-dd and the outdate will be in the same format.

 

 

$date = strtotime(date("Y-m-d", strtotime($indate)) . " +".$noofdays ." day");

 

return $date ;

 

}

 

But it returns a number !

 

 

Link to comment
Share on other sites

Yikes

 

cracked it - after 6 hours. Yippee. The solution (simple as always) was ...

 

function calcdate($indate, $noofdays)  {

// calculates a number of days from the supplied date, plus (no sign needed) or minus (give - sign in $noofdays

// Assumes that indate is yyyy-mm-dd and the outdate will be in the same format.

 

 

$date = strtotime(date("Y-m-d", strtotime($indate)) . " +".$noofdays ." day");

 

return date("Y-m-d", $date) ;

 

}

 

 

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.