convert a date string using PHP [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert a date string in a format like bellow using PHP,
2015-07-30T08:05:00.917Z
I try to convert using the bellow code,
echo str_replace('+00:00', '.917Z', gmdate('c', strtotime('2016-04-05 00:00:00')));
This will create 2016-04-05T00:00:00.917Z This is not correct,Is there any function to create date like this format?

There are two ways to create date from String.
1. date_create_from_format
$date=date_create_from_format("DATE_FORMAT",$dateString);
2. $date=new DateTime();
$date=$date->createFromFormat("DATE_FORMAT", $dateString);
Using following link to get your DATE_FORMAT.
http://php.net/manual/en/function.date.php

You can do it as #Alok has mentioned in his answer.
Or similarly using the following code.
$date = DateTime::createFromFormat('U.u', microtime(TRUE));
echo $date->format('Y-m-d\TH:i:s.u\Z');
Note that the microsecond mentioned in your question is of 3 digits. But generally php date formatting outputs microseconds in 6 digits precision.
Like the above code outputs 2016-04-06T09:24:50.830000Z
So I think you have to do some hack there if the digit precision is important.

Pass it through the function formatTime. This creates a new DateTime object in PHP called $date that can also easily be manipulated if you need. The examples below formats the date in two different ways, both work.
Procedural Style:
function formatTime($millis) {
$date = new DateTime($millis);
return date_format($date, 'l, jS F Y \a\t g:ia');
}
Object Oriented Style:
function formatTime($millis) {
$date = new DateTime($millis);
return $date->format('l, jS F Y \a\t g:ia');
}
The format for date can be found at this link:
http://php.net/manual/en/function.date.php
Hope this helps.

Related

Converting string of numbers to date in PHP [duplicate]

This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I'm trying to convert a string of numbers I am pulling from a db that looks like this '010219', representing January 2, 2019. I cannot find a way to convert this into 2019-01-02 using php, I just keep getting today's date from the functions I am trying.
Needs to be accomplished with no separators in original string.
There are a variety of ways to accomplish this, however the most concise is probably to use date_create_from_format.
An example is here:
$date = date_create_from_format('dmy', '010219');
This will output a Date as so:
echo date_format($date, 'Y-m-d');
Outputs: 2019-02-01
The date_create_from_format function accepts a parameter that defines the format of the date. In this case, the format is dmy which means:
d - Day of month as two-digit number (01-31)
m - Month of year as two-digit number (01-12)
y - Year as two-digit number
The documentation for date_create_from_format is here.
have you tried something like this?
<?php
$str="010219";
$date = DateTime::createFromFormat('dmy', $str);
echo $date->format('Y-m-d'); //2019-02-01
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Please see source: Converting string to Date and DateTime
split and concatenate with preg_replace
$newformat = preg_replace("/^(\d\d)(\d\d)(\d\d)$/","20$3-$1-$2","010219");

PHP convert a MM/DD/YYYY string to a MMDDYYYY [duplicate]

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;
?>

php format date with 2 characters [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
i have a date like this :
3/6/2014 7:20
I would like it to be formatted like
03/06/2014 07:20
What is the best practice to do that ?
As simple as :)
date("m/d/Y h:i");
Refer to Manual to see what these values represent. Um, let me bring those here.
m = Numeric representation of a month, with leading zeros
d = Day of the month, 2 digits with leading zeros
Y = A full numeric representation of a year, 4 digits
h = 12-hour format of an hour with leading zeros
i = Minutes with leading zeros
Your format is a bit ambigous, though I'm assuming since AM/PM isn't in there that the hours are in 24 hours format. Also not sure if the incoming date is day/month or month/day.
$date = new DateTime("3/6/2014 7:20");
echo $date->format('m/d/Y H:i');
Also not sure if the incoming date is day/month or month/day. But you can handle that with createFromFormat
$date = new DateTime::createFromFormat("n/j/Y G:i", "3/6/2014 7:20");
echo $date->format('m/d/Y H:i');
Read about date formats in the online docs. Also check out the DateTime book by the extension author to learn more.
Use the createFromFormat() function to read according to the specified format and output to the format you want.
http://www.php.net/manual/en/datetime.createfromformat.php
DateTime::createFromFormat -- date_create_from_format —
Returns new DateTime object formatted according to the specified format
Example:
$date = DateTime::createFromFormat('j/n/Y', '3/6/2014 G:i');
echo $date->format('m/d/Y h:i');

Convert DATETIME issue [duplicate]

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

Reformat datetime variable in PHP [duplicate]

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!

Categories