Jump to content

Pull elements from a date field


Recommended Posts

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

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




Edited by kongondo
Link to comment
Share on other sites

  • 4 years later...
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...