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
Related
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 5 years ago.
Need Solution For Providing Date And Time.
Have a look to DateTime::createFromFormat:
$dt = DateTime::createFromFormat('d M Y h:i:s O', '01 Feb 2018 01:01:12 +0530');
echo $dt->format("Y-m-d H:i:s");
try this
$date = "Thu, 01 Feb 2018 01:01:12 +0530";
echo date("Y-m-d H:i:s",strtotime($date));
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
Extreme PHP newbie here - I am trying to create a PHP variable that will be "CURRENT DATE + 7 Days"
Something like :
date('D-m-y H:i:s', strtotime(DateTime("+7 day"))
However, I need it to output in a format like this: "30 November 2015 09:00:00"
Any ideas?
Thanks in Advance!
You can check the manual for valid date formats and change your format string.
You're basically looking for date('j F Y H:i:s', strtotime("+7 day"))
Personally, I recommend working with DateTime if you're storing this in a variable and working with it, because it becomes more convenient to extract the formatted date from the object at your conveience any time without having to go back through date and strtotime each time. Also there are numerous other benefits like not losing timezone information during conversion or having to change global timezones that effect the conversion, etc...
Example
$date = new DateTimeImmutable; // today's date
echo $date->modify('+7 days')->format('j F Y H:i:s'); // 7 days from today
echo $date->modify('-7 days')->format('j F Y H:i:s'); // 7 days ago
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