Example 1: 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 2: monday tuesday wednesday => monday - wednesday php
function weekendDaysToString($days, $lang = 'en') {
$and = array('pt'=>'e', 'en'=>'and');
$strWeek = array(
'pt'=>array("Segunda", "Terça", "Quarta", "Quinta", "Sexta",
"Sábado", "Domingo"),
'en'=> array("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday")
);
$days = array_unique($days);
sort($days);
$seq = preg_replace_callback(
'/0246|024|025|026|135|146|246|02|03|04|05|06|13|14|15|16|24|25|26|35|36|46/',
function ($m) { return join( ',' , str_split($m[0]) ); },
implode('',$days)
);
$seq = preg_replace('/(\d)\d*(\d)/', '$1-$2', $seq);
$a = explode(',',$seq);
$last = array_pop($a);
$n = count($a);
$seq = $n? implode(", ",$a): $last;
if ($last && $n) $seq = "$seq $and[$lang] $last";
return preg_replace_callback(
'/\d/',
static function ($m) use (&$strWeek,$lang) {
return $strWeek[$lang][$m[0]];
},
$seq
);
}
print "\n".weekNumbers_toStr(array(6,1,2,3,6),'en');
print "\n".weekNumbers_toStr(array(0,1,2,3,6));
print "\n".weekNumbers_toStr(array(3,4,6),'pt');
print "\n".weekNumbers_toStr(array(3,4,6));
print "\n".weekNumbers_toStr(array(2,3,4,6));
print "\n".weekNumbers_toStr(array(3,5));
print "\n".weekNumbers_toStr(array(0,2,4,6));
print "\n".weekNumbers_toStr(array(0));
print "\n".weekNumbers_toStr(array());