AV1611 Posted June 23, 2006 Share Posted June 23, 2006 I have a date like 2006-06-21in a VARCHAR field.I need to subtract 1 day from it.$date='2006-06-21';if I do $date=$date-1;it give 2005How do I do it? Quote Link to comment https://forums.phpfreaks.com/topic/12742-date-math/ Share on other sites More sharing options...
paul2463 Posted June 23, 2006 Share Posted June 23, 2006 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 ithope this helps Quote Link to comment https://forums.phpfreaks.com/topic/12742-date-math/#findComment-48848 Share on other sites More sharing options...
AV1611 Posted June 23, 2006 Author Share Posted June 23, 2006 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)); Quote Link to comment https://forums.phpfreaks.com/topic/12742-date-math/#findComment-48856 Share on other sites More sharing options...
paul2463 Posted June 23, 2006 Share Posted June 23, 2006 [!--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 workedyou 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 workand in fact I have just tested it and it does work that way Quote Link to comment https://forums.phpfreaks.com/topic/12742-date-math/#findComment-48864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.