Jump to content


Photo

Date() math


  • Please log in to reply
3 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 23 June 2006 - 04:38 PM

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?



#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 23 June 2006 - 04:56 PM

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


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

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
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 23 June 2006 - 05:16 PM

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));


#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 23 June 2006 - 05:32 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
$basedate = strtotime("21 june 2006");
$finaldate = date("Y-m-d ", strtotime("-1 day", $basedate));
[/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
$basedate = strtotime("21 june 2006");
$finaldate = date("Y-m-d ", (strtotime("-1 day", $basedate)));

your method would work

and in fact I have just tested it and it does work that way
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users