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
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I can change the date and time but I issue is I save my date and time in database in this format Thursday 09th of July 2020 12:24:23 PM so I want to convert this in d-m-Y OR d-m-Y H:i:s Please help me how to I make this I search this related thread on stackoverflow but I couldn't achieve that ..
current I have tried this below code..
<?php
$orgDate = "Thursday 09th of July 2020 12:24:23 PM";
$newDate = date("d-m-Y", strtotime($orgDate));
echo "New date format is: ". $newDate. " (MM-DD-YYYY)";
?>
RESPONSE: New date format is: 01-01-1970 (MM-DD-YYYY)
Personally I would make sure the date is being saved in simplier format to database (DATETIME), then it's more accessible and even easy to use in database query when comparing dates etc.
But if you have no choice, this should do:
$orgDate = "Thursday 09th of July 2020 12:24:23 PM";
$dateTime = DateTime::createFromFormat('l dS * F Y h:i:s A', $orgDate);
echo $dateTime->format('Y-m-d');
DateTime parameters are explained in documentation: https://www.php.net/manual/en/datetime.createfromformat.php
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
How would I change the date 18/08/2018 10:46:15 pm to Sat 25, 10:46:15 pm in PHP. I am doing the following:
```
$OldDate = "18/08/2018 10:46:15 pm";
$NewDate = $OldDate->format(D-j, Y-H:I:S);
echo $NewDate;
?>```
However, it is not showing anything, am I formatting it correctly?
Your old date is just a string. If you want to format it, you need to create a DateTime object using that string the constructor.
You're also incorrectly specifying the format
$OldDate = new DateTime("18/08/2018 10:46:15 pm");
echo $OldDate->format("L-j, h:i:s A");
http://php.net/manual/en/class.datetime.php
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have standart sql datetime format: 2016-10-24 14:26:53.000
And i dont know how to convert into UTC format should be something like: 138853800000
Can be done in sql or php.
I tried something like:
$d = DateTime::createFromFormat('Y-m-d', 2016-10-24, new DateTimeZone('UTC'));
echo $d->getTimestamp().'<br>';
I works but after i add hours minutes it stops
Here is my two cents :
$d = new dateTime("2016-10-24 14:26:53.000", new DateTimeZone("UTC"));
echo $d->getTimestamp();
Output :
1477319213
echo $d->format('Y-m-d H:i:s.u P');
output :
2016-10-24 14:26:53.000000 +00:00
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 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');