AikenDrum Posted May 5, 2007 Share Posted May 5, 2007 Hi there forum I am a real newbie to php - been sort of forced into it by the boss. I have created 2 pages to query a database. There are 2 bits of date manipulation with which I am having a problem: Last Week and Last Month - when the user clicks Last week, I need a string produced. If today is 05/05/2007, the string I need produced is "29/04/2007...04/05/2007" which is the last 7 days. If the user clicks Last Month, I need just the number produced, so if this month is May, I need 4 produced. I have tried all sorts of stuff with mktime, but it just produces rubbish, here is an example of my code $reportType = $_POST["reportType"]; if ($reportType == 'Last Weeks Report') { $searchString = date('d/m/Y', mktime(0, 0, 0, date("m") , date("d") -8, date("Y")))."...".date('d/m/Y', mktime(0, 0, 0, date("m") , date("d") -1, date("Y"))); } Can anyone please show me what I have done wrong - any help much appreciated AikenD Quote Link to comment https://forums.phpfreaks.com/topic/50184-date-subtraction/ Share on other sites More sharing options...
mpharo Posted May 6, 2007 Share Posted May 6, 2007 if you want straight 30 days or 7 days then just use the strtotime() function... date("Y-m-d", strtotime("last week")); will get you 7 days ago from today..... date("Y-m-d", strtotime("last month")); will get you this day last month... Quote Link to comment https://forums.phpfreaks.com/topic/50184-date-subtraction/#findComment-246385 Share on other sites More sharing options...
Barand Posted May 6, 2007 Share Posted May 6, 2007 If I echo $searchstring from your code, I get this (today is 6 May, 2007) 28/04/2007...05/05/2007 so the -8 should be -7, giving 29/04/2007...05/05/2007 ie 7 days commencing 29 Apr (only 30 days in April !) But if you hope to search a database with a date range like that then you are out of luck. You need to format as yyyy-mm-dd if the dates are held in a DATE field. dd/mm/yyyy format cannot be used in date comparisons or searches. Quote Link to comment https://forums.phpfreaks.com/topic/50184-date-subtraction/#findComment-246386 Share on other sites More sharing options...
AikenDrum Posted May 6, 2007 Author Share Posted May 6, 2007 Hi There - many thanks for the tip - For the week concat I used date("d/m/Y", strtotime("last week"))."...".date("d/m/Y"); For the month number I used date("m", strtotime("last month")); Excellent results - I did not understand how powerful strtotime was As to the DB search, the DB engine I am using does accept this sort of search on a date field Many thanks guys AikenD Quote Link to comment https://forums.phpfreaks.com/topic/50184-date-subtraction/#findComment-246507 Share on other sites More sharing options...
Barand Posted May 6, 2007 Share Posted May 6, 2007 Shouldn't it be echo date("d/m/Y", strtotime("last week"))."...".date("d/m/Y", strtotime('yesterday')); otherwise you have an 8 day week ending today. Quote Link to comment https://forums.phpfreaks.com/topic/50184-date-subtraction/#findComment-246513 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.