I have a variable that outputs the date in the following format:
Which is 2013 - Aug - 13 - 18:30
I am using this to retrieve the day name but it is getting the wrong days:
$dayname = date('D', strtotime($longdate));
Any idea what I am doing wrong?
This is what happens when you store your dates and times in a non-standard format. Working with them become problematic.
$datetime = DateTime::createFromFormat('YmdHi', '201308131830'); echo $datetime->format('D');
See it in action
You can not use
strtotime as your time format is not within the supported date and time formats of PHP.
Therefor, you have to create a valid date format first making use of createFromFormat function.
//creating a valid date format $newDate = DateTime::createFromFormat('YmdHi', $longdate); //formating the date as we want $finalDate = $newDate->format('D');
Your code works for me.
$input = 201308131830; echo date("Y-M-d H:i:s",strtotime($input)) . "\n"; echo date("D", strtotime($input)) . "\n";
2013-Aug-13 18:30:00 Tue
However if you pass
201308131830 as a number it is 50 to 100x larger than can be represented by a 32-bit integer. [dependent on your system's specific implementation] If your server/PHP version does not support 64-bit integers then the number will overflow and probably end up being output as a negative number and
date() will default to
Jan 1, 1970 00:00:00 GMT.
Make sure whatever source you are retrieving this data from returns that date as a string, and keep it as a string.
Your code works for me
$date = '15-12-2016'; $nameOfDay = date('D', strtotime($date)); echo $nameOfDay;
Use l instead of D, if you prefer the full textual representation of the name