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);
Related
This question already has answers here:
Getting date format m-d-Y H:i:s.u from milliseconds
(18 answers)
Closed 2 years ago.
How to produce this kind of date string in PHP ?
this sample date string below is coming from a postgresql output
2020-10-20 14:44:37.060966+08
I already tried something like
date('Y-m-d H:i:sO',strtotime('now'));
and
date('Y-m-d H:i:sOT',strtotime('now'));
but still i cannot produce the part starting from the decimal point to the end of my sample string.. so how to generate that format with decimal and + something something right after the seconds in PHP ?
That part is microsecond, you can use this format Y-m-d H:i:s.uO to add microsecond.
One thing to note that microsecond format with date() will always be 000000. Use DateTime::format() instead:
$date = new DateTime();
echo $date->format('Y-m-d H:i:s.uO');
Output:
2020-12-07 03:17:37.011493+0100
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");
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
This question already has an answer here:
Date in a URL dd/mm/yyyy
(1 answer)
Closed 5 years ago.
I am confused as to why my date is getting converted to unix default before entry into mysql. I am sure the code is correct but cannot see why this is not working. It should convert the date that I post to script.
I would be grateful if someone could check the code and point out my error. Many thanks.
Post: 22/08/2017 05:03:29 Output:1970-01-01 12:00:00
$date = $_POST['datetimepicker'];
$parsedDate = date('Y-m-d h:i:s', strtotime($date));
d/m/Y is not one of the date formats recognized by the PHP date parser.
Given the number of digits in the date components, the parser assumes m/d/Y and because 22 is not a valid month number it fails and strtotime() returns 0.
You can use DateTime::createFromFormat() to tell the parser what format do you use:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/08/2017 05:03:29');
echo($date->format('Y-m-d H:i:s'));
# 2017-08-22 05:03:29
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
hello,
we have this function to convert a timestamp to datetime:
$datetime = date('Y-m-d H:i:s', $timestamp);
is there a function to do the opposite?
datetime to timestamp.
Thanks
$timestamp = strtotime($datetime);
Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.
Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.
EDIT:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.
$timestamp = strtotime('12-04-2010');