Derbyshire10 Derbyshire10 - 3 years ago 91
PHP Question

php calculation multiplication for a date

Can someone please help me, I am trying to make this work.
If the date is a Sunday then I want it to times hourly rate by 1.5.

<?php
date_default_timezone_set('Europe/London');
$date ='2017/09/24';
$Hours ='';// removed to keep private
$Hourly_rate='';// removed to keep private
$sundaypay = $Hourly_rate*1.5;
if(date('w', strtotime($date)) == 7):
$pay = ($Hours * $sundaypay);
else:
$pay = ($Hours * $Hourly_rate);
endif;
?>


it is not working and just outputs the else even though 2017/09/24 was a Sunday. Does anyone know why

if(date('w', strtotime($date)) == 7):


does not work?

<?php echo $pay;?>

Answer Source

Open up a php manual for date.

There you will see that w format option is described as

Numeric representation of the day of the week: 0 (for Sunday) through 6 (for Saturday)

See? There's no 7, just numbers from 0 to 6.

Either compare to 0, or use N format option, which is:

ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0): 1 (for Monday) through 7 (for Sunday)

if(date('N', strtotime($date)) == 7)
// or
if(date('w', strtotime($date)) == 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download