0

I'm using some custom fields in WordPress and I have a custom field called date-of-event which can be used as a date picker from the back-end which is ideal for my client.

Client selects the date in the back-end:

Selecting a date

I get the value using:

$date = <?php get_post_meta(get_the_ID(), 'wpcf-date-of-event', true); ?>

However, the date is returned as a single line.

1455100200

How can I convert this single line of data into a readable date and time?

I've looked up http://php.net/manual/en/datetime.createfromformat.php but I'm not too sure what format is being returned in the first place?

WebDevDanno
  • 1,122
  • 2
  • 22
  • 50
  • You can use http://php.net/manual/en/function.date.php date function for example `date('Y-m-d',1455100200)` – Naumov Mar 03 '16 at 11:14

3 Answers3

2

It's a Unix Timestamp, so you can get the data by doing the following:

$d = DateTime::createFromFormat('U', '1455100200');

So just replace the number with your variable $date

You can then manipulate the DateTime object as you would like, for storing you probably want the following format:

$d->format('Y-m-d H:i:s');

Example output if you were to print this:

2016-02-10 10:30:00

Hope it helps.

Mikey
  • 2,606
  • 1
  • 12
  • 20
  • Ah you're a good egg Pigeon! Will mark as correct in 3 mins. Thanks, I asked a question and learned a lot =] – WebDevDanno Mar 03 '16 at 11:23
  • @WebDevDanno Glad I could help, just for future reference you can find a full list of formats [here](http://php.net/manual/en/function.date.php), these can all be used with the `->format()` method for a `DateTime` object. It gives you a lot of freedom for manipulating dates. ^^ – Mikey Mar 03 '16 at 11:46
0

It is Unix timestamp: 1455100200 = Wed, 10 Feb 2016 10:30:00 GMT

B-and-P
  • 1,693
  • 10
  • 26
0

The single line you're referring to is what is known as a unix timestamp. It is the number of seconds since Jan 01 1970. (UTC).

To make use of it you can do the following:

$timestamp = get_post_meta(get_the_ID(), 'wpcf-date-of-event', true);
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y-m-d H:i:s');
Tom Wright
  • 2,841
  • 4
  • 22
  • 30