I am using Advanced Custom fields in wordpress to input the start and end date of a particular tour. That date is formated mmddyy (10052013).
I'm trying to use PHP to format the date. The code is as follows:
<?php
$date = get_field('start_date');
$y = substr($date, 4, 4); $m = substr($date, 0, 2); $d = substr($date, 2, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
// format date (November 11th 1988)
echo date('F n, Y', $time);
?>
<?php
if(get_field('end_date')){
$date = get_field('end_date');
$y = substr($date, 4, 4); $m = substr($date, 0, 2); $d = substr($date, 2, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
// format date (November 11th 1988)
echo ' - ' . date('F n, Y', $time);
}
?>
But when the dates are rendered on the page, for some reason, the date is always "October 10, 2013" and I don't know why. You can see it in action here: http://onedirectionconnection.com/tester/?projects=take-me-home-tour
I output the database values as they're saved normally to show that they're definitely saved correctly. It's something going wrong in the way I coded the PHP.
Could anyone help me out here?
Try:
echo ' - ' . date('F d, Y', $time);
You're creating the date correctly, but both F
and n
refer to the month.
Reference: http://www.php.net/manual/en/datetime.createfromformat.php
See it working here: https://eval.in/52927
Probably this link have everything you need to know about formatting date and time in WP
n
is numeric month
j
is day numeric without leading zero, and what you're looking for
echo ' - ' . date('F j, Y', $time);