I have three pieces of information extracted from my database, the year, the number of week (1-52), and the day of week (mon, tue, wed). And I need to convert it in to a date.
How would I go about doing that in php?
I have three pieces of information extracted from my database, the year, the number of week (1-52), and the day of week (mon, tue, wed). And I need to convert it in to a date.
How would I go about doing that in php?
Try this :
$yr="2014";
$week="01";
$day = "Mon";
echo date("Y-m-d", strtotime($yr."W".$week."D ".$day));
PHP fiddle:http://phpfiddle.org/main/code/nzg-ws6
Did a little digging and it looks like the day will need to be in numerical format for the strtotime conversion to work properly. Using a string such as 'Monday' won't work.
$yr = "2014";
$week = "01";
$day = "1"; //Monday
echo date("Y-m-d", strtotime($yr . "W" . $week . $day)); // "2014W011"
Note that I didn't include the 'D' prefix for the day as this looks to be following ISO8601.
Swapping your days to numbers can be done a few ways: You could make an array Monday to Sunday and use the array key, or maybe a little easier you could do something like this:
$day date("N", strtotime("Monday")); // will echo 1
So the full code could look something like this:
$yr = "2014";
$week = "01";
$day = date("N", strtotime("Monday")); //
echo date("Y-m-d", strtotime($yr . "W" . $week . $day)); // "2014W011"
Here is a demo to check: http://3v4l.org/EllgQ
This converts you $day value to desired day format. try this
$yr="2011";
$week="01";
$day="Monday";
$day=date("D",strtotime($day));
echo date("Y-m-d", strtotime($yr."W".$week."D ".$day));;