Jump to content

[SOLVED] Problem With Day & Dates


Tigggger

Recommended Posts

Hi All,

 

I wrote a page that lists all jobs from my database for a week (Saturday to Friday)

 

This has worked perfectly since last September, but something about the change over between march and april seems to have broken it.

 

It works perfectly on the current week returning

$startdate 2007-03-31

$enddate 2007-04-07 (I use < $enddate later so this is correct)

 

but now when I go back a week it shows

$startdate 2007-03-23

$enddate 2007-03-30

instead of 24 and 31

 

Any ideas what could be causing this, code below.

 

Regards

Tigggger

 

  $weekposition = strpos($_SERVER['REQUEST_URI'],"week=")+5;
  $week = substr($_SERVER['REQUEST_URI'],$weekposition,2); //gets week number from url
  $dmy = date("Y-m-d");
  $cweek = date('W');
  $dDate = strtotime(trim($dmy));
  while (trim(date("w",$dDate)) !== '6') {
  $dDate = strtotime("-1 day", $dDate);
  } // this goes backwards until it reaches saturday which is the start day I require
  if (!isnum($week)) { $week = $cweek; }
  if ($week < $cweek ) { 
  $weeks = $cweek - $week;
  $calc = $weeks * 604800;
  $dDate = $dDate - $calc;
  }
  elseif ($week > $cweek ) { 
  $weeks = $week - $cweek;
  $calc = $weeks * 604800;
  $dDate = $dDate + $calc;
  }
  $dRunDate = $dDate;
  $startdate = date('Y-m-d',$dRunDate);
  $dEndDate = strtotime("+7 day", $dDate);
  $enddate = date('Y-m-d',$dEndDate);

Link to comment
https://forums.phpfreaks.com/topic/45137-solved-problem-with-day-dates/
Share on other sites

Try this

<pre>
<?php
$startdate = '2007-03-31';
$enddate = '2007-04-07';

$prevstart = date ('Y-m-d', strtotime("$startdate -7 days"));
$prevend = date ('Y-m-d', strtotime("$enddate -7 days"));

printf ('Prev week : %-15s-%15s%s', $prevstart, $prevend, "\n");  
printf ('This week : %-15s-%15s%s', $startdate, $enddate, "\n");
?>
</pre>

 

I'm guessing that your calculation using number of seconds is taking it to 11pm on the day before because of the daylight saving time change.

Thanks Barand,

 

Changed my input from ?week=11 to ?week=2007-03-31

 

This allowed me to remove all the code above and just use

 

  $startdate = substr($_SERVER['REQUEST_URI'],$weekposition,10);
  $enddate = date ('Y-m-d', strtotime("$startdate +7 days"));
  $prevdate = date ('Y-m-d', strtotime("$startdate -7 days"));

 

These 3 give me enough to get a previous week ?week=$prevdate and a next week ?week=$enddate

 

Thanks again, much appreciated

 

Regards

Tigggger

 

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.