K Ahir
K Ahir

Reputation: 395

PHP strtotime(last day) function related issue

I need to show weekly sales report from every last Saturday 20.00.01 to next Saturday 20.00.00

I have got last Saturday and next Saturday info from current day using below code.

<?php
$dt_week_start_date = date('Y-m-d 20:00:01',strtotime("last Saturday")); 
$dt_week_end_date = date('Y-m-d 20:00:00',strtotime("next Saturday"));
?>

For Example: right now server date-time is Friday, 2014-08-15 05:43:49 then below code gives me correct result.

<?php print(date("d-M-Y H:i:s",strtotime($dt_week_start_date)));?> TO 
<?php print(date("d-M-Y H:i:s",strtotime($dt_week_end_date)));?>

Output: 09-Aug-2014 20:00:01 TO 16-Aug-2014 20:00:00

Now For Example: right now server date-time is Saturday, 2014-08-16 05:43:49 then below code gives me incorrect result.

<?php print(date("d-M-Y H:i:s",strtotime($dt_week_start_date)));?> TO 
<?php print(date("d-M-Y H:i:s",strtotime($dt_week_end_date)));?>

Output: 09-Aug-2014 20:00:01 TO 23-Aug-2014 20:00:00

So right now it's Saturday, 2014-08-16 05:43:49 so it's less than value of $dt_week_end_date variable (2014-08-16 20:00:00) so it should display 16-Aug-2014 20:00:00 instead of 23-Aug-2014 20:00:00.

Please let me know what is wrong with my code? Thank you in advance.

Upvotes: 0

Views: 84

Answers (2)

MH2K9
MH2K9

Reputation: 12039

Try something like below

$dt_week_start_date = strtotime("last Saturday");
$from = date('d-M-Y 20:00:01', $dt_week_start_date);
$to = date('d-M-Y 20:00:01', $dt_week_start_date + (7  *24 * 60 * 60));
echo $from.' To '.$to;

Output:

09-Aug-2014 20:00:01 To 16-Aug-2014 20:00:01

Upvotes: 0

Satish Sharma
Satish Sharma

Reputation: 9625

try this

<?php
$today = date('d-M-Y');
$today_time = strtotime($today);

$dt_week_start_time = strtotime("last saturday")+((20*3600)+ 1);
$dt_week_start_date = date('d-M-Y G:i:s', $dt_week_start_time);

$dt_week_end_time = $dt_week_start_time + (7*3600*24) - 1;
$dt_week_end_date = date('d-M-Y G:i:s', $dt_week_end_time);

echo "Today : ".$today;
echo "\n";
echo "Last Saturday : ".$dt_week_start_date;
echo "\n";
echo "Next Saturday : ".$dt_week_end_date;
?>

Output:

Today : 16-Aug-2014
Last Saturday : 09-Aug-2014 20:00:01
Next Saturday : 16-Aug-2014 20:00:00

Demo

Upvotes: 1

Related Questions