Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.