Jump to content

Date Subtraction


AikenDrum

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/50184-date-subtraction/
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/50184-date-subtraction/#findComment-246386
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/50184-date-subtraction/#findComment-246507
Share on other sites

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.