icreation Posted November 8, 2015 Share Posted November 8, 2015 I have a field type datetime that I have configured in PW backend. However, I want to split this down so that I can display the day and month/year separately. echo $page->datefield; Gives me the full date that looks like '8 November 2015' How do I express the PHP code to just echo the '8'. Then just echo the 'November 2015' ? Link to comment Share on other sites More sharing options...
LostKobrakai Posted November 8, 2015 Share Posted November 8, 2015 You can get the timestamp of the date when accessing the unformatted field data like so: $ts = $page->getUnformatted("datefield"); $day = date("d", $ts); $monthYear = date("F Y", $ts); 2 Link to comment Share on other sites More sharing options...
kongondo Posted November 8, 2015 Share Posted November 8, 2015 (edited) Something like this should do it. But you'd want to set timezones if necessary and maybe use PHP DateTime Class for more complex needs. Read below links PHP date $d = strtotime($page->datefield); echo date('d', $d) . '<br>';//e.g 8 echo date('F Y', $d);//e.g. November 2015 http://php.net/manual/en/function.date.php http://php.net/manual/en/function.strtotime.php http://php.net/manual/en/class.datetime.php Edited November 8, 2015 by kongondo Link to comment Share on other sites More sharing options...
LostKobrakai Posted November 8, 2015 Share Posted November 8, 2015 @kongondo $dateStr might be confusing, as it's NOT a string anymore 1 Link to comment Share on other sites More sharing options...
kongondo Posted November 8, 2015 Share Posted November 8, 2015 What LostKobrai said + you'd want to first check if there was a value saved in your date field otherwise you might get a nice 1 Jan 1970 Link to comment Share on other sites More sharing options...
SiNNuT Posted November 8, 2015 Share Posted November 8, 2015 iirc PHP's date() and DateTime() functions are not locale aware, so strftime() could also be of interest. http://php.net/manual/en/function.strftime.php Link to comment Share on other sites More sharing options...
icreation Posted November 9, 2015 Author Share Posted November 9, 2015 Thanks for the replies. Appreciated. Link to comment Share on other sites More sharing options...
Chris Ernovsky Posted May 9, 2020 Share Posted May 9, 2020 On 11/8/2015 at 8:40 PM, LostKobrakai said: You can get the timestamp of the date when accessing the unformatted field data like so: $ts = $page->getUnformatted("datefield"); $day = date("d", $ts); $monthYear = date("F Y", $ts); @LostKobrakai, thank You for this! I have to split a date/time field into day of week, day, month and time in a loop (an archive of events) and couldn't figure it out. I saw this function at forums several times before but didn't know I can use it with date. Awsome! Thanks again for the tip. ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now