Trying to print the date in "Friday, 15 November 2019" format using the following PHP code using the standard ECHO statement:
$mydate='2019-11-15';
echo "Raw mydate printed through echo is $mydate<br>";
echo "Formatted mydate through echo is ". date('l, d F Y', strtotime('$mydate'));
However, the output (produced as follows) is strangely taking the formatted date to the first date in UNIX (ie nullifying the $mydate variable value). Output is as follows:
Raw mydate printed through echo is 2019-11-15
Formatted mydate through echo is Wednesday, 31 December 1969
Does PHP ECHO statement does not recognize formatting statements? If it does, then what am I doing wrong in the above code?
PS: I have already tried using " <?php echo htmlspecialchars($row["date"], ENT_QUOTES, "UTF-8"); ?>
" as mentioned in Format date within an echo , but that does not help either. Also used the " date_create
" statement (like $mydate = date_create($mydate);
) both before and after the formatting statement, but in all cases the formatted date is going to 31Dec1969. What am I doing wrong here, or does ECHO not support formatting at all?
Thanks
Try like this:
$mydate='2019-11-15';
echo "Raw mydate printed through echo is $mydate<br>";
echo "Formatted mydate through echo is ". date('l, d F Y', strtotime($mydate));
Your mistake was the single quotes in strtotime
. If you pass a variable in single quotes it is taken literally. So it actually tries to parse $mydate
as a literal string, it does not take the value itself. If you want to pass the variable in a string, you should use double quotes "
.
You can read more about it here .
Just make the following change, you are passing $mydate
as string to the date function.
Remove single quote as shown below:
$mydate='2019-11-15';
echo "Raw mydate printed through echo is $mydate<br>";
echo "Formatted mydate through echo is ". date('l, d F Y', strtotime($mydate));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.