This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].
Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?
I tried with datetime::createformat but It doesn't works.
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))
If you want another method. Even if i would prefer the one already mentioned.
Output 05/02/2014 17:12:48
PHP 5.3 and older:
$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');
PHP 5.4+:
$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');
with the variable:
$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
If I understand you right, $reportDb["reportDate"] is already a DateTime object.
If so, all you need is ...
$reportDb["reportDate"]->format('d/m/Y H:i:s');
Cheers!
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.
I'm having a date format like this:
2010-09-21T00:00:00+03:00
how can i convert it to 'Y-m-d H:i:s' format?
I've tried following code but I don't think it's working:
$date = new DateTime('2010-09-21T20:00:00+03:00');
$status_date = $date->format('Y-m-d H:i:s');
It prints time like this: 2010-09-21 20:00:00
It prints exactly want you want ! So what's wrong ?
You can also use Carbon and to like this :
Carbon::parse('2010-09-21T00:00:00+03:00')->toDateTimeString();
UPDATE
Be careful, you said you want midnight but you wrote 8pm in your php !
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a string representing a date. It's format is MM/DD/YYYY. I need to submit it via API to a service that requires MMDDYYYY. Should I bother with overloading this string onto some date class and 'export' as MMDDYYY or just delete the "/" sub-string wherever I find it?
Any other neat way of doing that I wasn't aware of?
Since you just need to remove the '/' I would do that with the following code, it will eliminate the overhead of parsing the date.
$newDate = str_replace ('/', '', $oldDate);
You can use the DateTime object in PHP (http://php.net/manual/en/class.datetime.php)
Here is how I would do it:
<?php
$myDate = '05/15/2015';
$date = DateTime::createFromFormat('m/d/Y H:i:s', "$myDate 00:00:00");
$newDate = $date->format('mdY');
echo $newDate . PHP_EOL;
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I've had a very good search on this topic but haven't managed to turn up a definitive answer that gives me what i need. What i'm trying to do is convert as follows (in PHP):
From: '2014-04-16 08:22:00.000'
To: '16/04/2014 08:22'
And then back again, does anyone have an idea as to how this might be achieved? I not using seconds so i don't need that portion of the format only d/m/Y m:i. The original format comes from an MSSQL DB datetime field and the converted format will show in a input field.
Many thanks in advance.
DateTime::createFromFormat is your friend.
$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2014-04-16 08:22:00.000');
echo $date->format('d/m/Y H:i');
OR
$date1 = new DateTime('2014-04-16 08:22:00.000');
echo $date1->format('d/m/Y H:i');
From DATETIME to PHP Date Formting:
date("WHATEVER", strtotime($mysql_result->date))
To DATETIME from date:
date('Y-m-d H:i:s', strtotime($time))
https://php.net/manual/en/function.date.php
https://php.net/manual/en/function.strtotime.php
Try to find your answer on google before:
http://www.php.net/manual/en/datetime.formats.date.php
or
Convert one date format into another in PHP