Converting string of numbers to date in PHP [duplicate] - php

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");

Related

String to Date or Date and Time [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449

How to add day to a string in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
My current Date format is in MM/YY. I Need to default days to my format in php.
For example:
12/2009 -> 07/12/2009
I tried this code:
$currdate = '07/'.$currdate;
$newFormat = date('d-M-Y',strtotime($currdate));
But the new format is wrong, it output 12/07/2009.
----------------- Edit -----------------------------
I have tried **DateTime::createFromFormat**.Since my $currdate date format has only month and year its not accepting.I am getting a fatal error.
strtotime expects an american date format. Use datetime::createFromFormat instead:
$date = DateTime::createFromFormat('d/m/Y', $currdate);
echo $date->format('Y-m-d');
Edited to better explanation:
When you use date with slashes(/), PHP strtotime will think it is in m/d/Y format, the american way.
If you use dash (-) it will assume d-m-Y format.
If you use dot (.) it will assume Y.m.d format.

Convert Date(*) number to time (d/m/Y) in php [duplicate]

This question already has answers here:
Convert timestamp to readable date/time PHP
(14 answers)
Closed 8 years ago.
simple question, I have a date, I think it a Timestamp, I'm not sure but it looks something like that:
'/Date(1403024400000)/'
How do I convert that to D/M/Y Just a regular Europe date.
Basically you need to extract the timestamp from the string first. This can be done using a regex.
The timestamp is a UNIX timestamp with millisecond accuracy. Therefore you need to remove the last 3 digits from it before you can convert it to the desired format.
Like this:
$string = '/Date(1403024400000)/';
// retrieve the timestamp with a regex
preg_match('/Date\((.*?)\)/', $string, $matches);
$timestamp = $matches[1];
// the timestamp contains milliseconds. remove the last 3 digits
$timestamp = substr($timestamp, 0, -3);
// convert to d/m/Y using date()
echo date('d/m/Y', $timestamp);
~
Output:
17/06/2014
Btw, there is no "regular" European date format. In germany for example we are using d.m.Y (using dots as separator)
Chop off the last three numbers (as it doesn't seem you care about that anyway) and do something like this
echo date('d/m/Y', 1403024400);

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');

Date format conversion results wrong date [duplicate]

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

Categories