php datetime get day of week code example
Example 1: format('Y-m-d H:i:s'); ?>
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>
Example 2: php check weekday of date
$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
Example 3: php get day of week
date('w');
if(date('w') == 1){
echo "its monday baby";
}
Example 4: php week of a date
Things to be aware of when using week numbers with years.
<?php
echo date("YW", strtotime("2011-01-07"));
echo date("YW", strtotime("2011-12-31"));
echo date("YW", strtotime("2011-01-01"));
?>
BUT
<?php
echo date("oW", strtotime("2011-01-07"));
echo date("oW", strtotime("2011-12-31"));
echo date("oW", strtotime("2011-01-01"));
?>
Reason:
Y is year from the date
o is ISO-8601 year number
W is ISO-8601 week number of year
Conclusion:
if using 'W' for the week number use 'o' for the year.
Example 5: week starting date and end date in php
<?php$week=29;$year=2017;function getStartAndEndDate($week, $year) { $dateTime = new DateTime(); $dateTime->setISODate($year, $week); $result['start_date'] = $dateTime->format('d-M-Y'); $dateTime->modify('+6 days'); $result['end_date'] = $dateTime->format('d-M-Y'); return $result;}$dates=getStartAndEndDate($week,$year);print_r($dates);?>