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
https://forums.phpfreaks.com/topic/177393-solved-manipulating-dates/
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) ;

 

}

 

 

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.