This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
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:
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?
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.
It is Unix timestamp:
1455100200 = Wed, 10 Feb 2016 10:30:00 GMT
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');
Related
This question already has an answer here:
Date in a URL dd/mm/yyyy
(1 answer)
Closed 5 years ago.
I am confused as to why my date is getting converted to unix default before entry into mysql. I am sure the code is correct but cannot see why this is not working. It should convert the date that I post to script.
I would be grateful if someone could check the code and point out my error. Many thanks.
Post: 22/08/2017 05:03:29 Output:1970-01-01 12:00:00
$date = $_POST['datetimepicker'];
$parsedDate = date('Y-m-d h:i:s', strtotime($date));
d/m/Y is not one of the date formats recognized by the PHP date parser.
Given the number of digits in the date components, the parser assumes m/d/Y and because 22 is not a valid month number it fails and strtotime() returns 0.
You can use DateTime::createFromFormat() to tell the parser what format do you use:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/08/2017 05:03:29');
echo($date->format('Y-m-d H:i:s'));
# 2017-08-22 05:03:29
This question already has answers here:
How to get GMT date in yyyy-mm-dd hh:mm:ss in PHP
(6 answers)
Closed 6 years ago.
I am using mssql db with php. Which PHP function can return the current datetime. (i.e.) I want the current date and time to be saved in the following format say for example,
2016-07-04 11:10:05.000
Thanks!
Use date->format http://php.net/manual/it/function.date.php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s.u');
12-hour format
date("Y-m-d h:i:s.u")
24-hour format
date("Y-m-d H:i:s.u")
With "date" you can format the output and with "time" you get the current unix time.
Example
echo date("Y-m-d H:i:s.u", time());
Documentation: http://php.net/manual/it/function.date.php
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I've got a date in DD.MM.YYYY h:mm a format, how could i parse it in php or laravel to store it in a datetime field in mysql database?
Here's the date i want to parse: 29.08.2015 11:00 pm
I tried to parse to parse it like this:
date_create_from_format('DD.MM.YYYY h:mm a','29.08.2015 11:00 pm');
But it didn't work, it returns false...
$date = date_create_from_format('j.m.Y h:i a','29.08.2015 11:00 pm');
echo $date->format('Y-m-j H:i:s');
Outputs 2015-08-29 23:00:00
new DateTime(string date) converts a string to a date object without caring the format. You can pass '29-8-2015','29 Aug 2015'.. etc as $datestring. No need to define the format of input.
$datestring='29.08.2015 11:00 pm'; //defined date as string
$datetime = new DateTime($datestring); //create datetime of defined date
$datetime = $datetime->format('Y-m-d H:i:s'); //reformat to the format we need
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I tried converting
12-18-1997
to
18-12-1997
with this code
$new_date = date('d-m-Y', strtotime('12-18-1997'));
but it results in 18-12-1969
If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.
Use DateTime instead of strtotime():
$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');
You can see from this demo that it prints:
18-12-1997
strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:
php > var_dump(strtotime('12-18-1997'));
bool(false)
Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.
If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:
$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);
This question already has an answer here:
Converting facebook time to human readable time with PHP
(1 answer)
Closed 9 years ago.
I am using time() command in php to store a timestamp value for every time database is updated i.e whenever a value is updated in database a timestamp is added in a time.
For eg: when a change was made to database yesterday night, the value added was 1368132319.
I know the the time() commands returns the no of seconds elapsed from jan 1 1970.
Now what i want to do is convert these no of seconds into a user understandable form which can be displayed on an html page. Like these seconds are converted to date and time.
How do i do that? i cannot think of a logic to implement it. googled it but to no avail
Pretty simple thing, simply use datetime
$date = date_create();
date_timestamp_set($date, 1171502725);
echo date_format($date, 'U = Y-m-d H:i:s') . "\n";
All in the manual http://php.net/manual/en/datetime.settimestamp.php
you can see the demo link....
<?php
$time_in_seconds = 1368132319;
$format = 'Y-m-d H:i:s';
echo date($format,$time_in_seconds);
?>
You can use the date() function. Example:
$timestamp = 1368132319; // in your case the value from the DB
echo date('Y-m-d', $timestamp); // 2013-05-09
echo date('Y-m-d H:i:s', $timestamp); // 2013-05-09 23:45:19
simply you can use below
<?
echo date("D-M-Y",$strtime);
?>
there are various format available for DATE function in PHP you can use required one.