CruisePlus Posted January 6, 2017 Share Posted January 6, 2017 (edited) Hi All!This is my second week of PHP. I'm going through a PHP course at the moment so i'm pretty certain i've made a stupid mistake somewhere! I'm trying to get a date that's entered in my CMS panel to show on the front-end. I've defined the variable departure_date --> got the raw data --> created a new date object --> tried to display the date object in the format j M Y. I believe there is something wrong with how i am defining the date object in my echo statement. I forgot to mention this is a wordpress website i'm working on with ACF custom fields if that helps. $departure_date = get_field('departure_date', false, false); $departure_date = new DateTime($departure_date); $cruise_line = get_field('cruise_line'); $ship = get_field('ship'); if(get_field('ship')) { echo 'Departs' . $departure_date->format('j M Y') . get_field('cruise_line') . ' | ' . get_field('ship'); } Edited January 6, 2017 by CruisePlus Quote Link to comment https://forums.phpfreaks.com/topic/302887-php-date-formatting/ Share on other sites More sharing options...
cyberRobot Posted January 6, 2017 Share Posted January 6, 2017 What does the code echo now? Have you tried outputting the various variables to see if you are getting expected values? Quote Link to comment https://forums.phpfreaks.com/topic/302887-php-date-formatting/#findComment-1541130 Share on other sites More sharing options...
CruisePlus Posted January 6, 2017 Author Share Posted January 6, 2017 What does the code echo now? Have you tried outputting the various variables to see if you are getting expected values? The variable cruise_line and ship are output correctly if $departure_date->format('j M Y') is removed Quote Link to comment https://forums.phpfreaks.com/topic/302887-php-date-formatting/#findComment-1541131 Share on other sites More sharing options...
cyberRobot Posted January 6, 2017 Share Posted January 6, 2017 What does $departure_date->format('j M Y') output? Quote Link to comment https://forums.phpfreaks.com/topic/302887-php-date-formatting/#findComment-1541134 Share on other sites More sharing options...
Barand Posted January 6, 2017 Share Posted January 6, 2017 (edited) What is the date format used in the database table? ie What do you get if you echo $departure_date immed after the get_field()? // MySQL DATE type $date = '2017-01-06'; $dt = new DateTime($date); echo $dt->format('j M Y') . '<br>'; // Unix epoch integer type $date = 1483660800; $dt = new DateTime("@$date"); echo $dt->format('j M Y') . '<br>'; Edited January 6, 2017 by Barand add code Quote Link to comment https://forums.phpfreaks.com/topic/302887-php-date-formatting/#findComment-1541135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.