Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

Date() math

Recommended Posts

I have a date like

2006-06-21

in a VARCHAR field.

I need to subtract 1 day from it.

$date='2006-06-21';

if I do

$date=$date-1;

it give

2005

How do I do it?

Share this post


Link to post
Share on other sites
I had the same problem as this a while ago and I had to use strtotime to make it work, this is how I did it

[code]

$basedate = strtotime("21 june 2006");
$plusdate = strtotime("-1 day", $basedate);
$finaldate = date("Y-m-d ", $plusdate);
echo ($finaldate);
[/code]
produces
[code]
2006-06-20
[/code]

if you look at:-
[a href=\"http://uk.php.net/manual/en/function.date.php\" target=\"_blank\"]http://uk.php.net/manual/en/function.date.php[/a] - it will tell you all bout the different ways it can represent dates "Y-m-d " produces a date as in your origonal post 2006-06-21

[a href=\"http://uk.php.net/strtotime\" target=\"_blank\"]http://uk.php.net/strtotime[/a] lets you know what this does and how it does it

hope this helps

Share this post


Link to post
Share on other sites
thanks a million... I was close, but I tried to do step 2 & 3 at once.. got an error

//wrong way to do it...

$basedate = strtotime("21 june 2006");
$finaldate = date("Y-m-d ", strtotime("-1 day", $basedate));

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
[code]
$basedate = strtotime("21 june 2006");
$finaldate = date("Y-m-d ", strtotime("-1 day", $basedate));
[/code]
[/quote]

if you had done your method but with an extra set of parenthases ( spelt wrong but hey ho) it may have worked
you need the final variable in the date function to be completed before the date function finishes its call so if you would have done
[code]
$basedate = strtotime("21 june 2006");
$finaldate = date("Y-m-d ", (strtotime("-1 day", $basedate)));
[/code]

your method would work

and in fact I have just tested it and it does work that way

Share this post


Link to post
Share on other sites

×

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.