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.
Related
This question already has answers here:
PHP Carbon determine date type from string format
(2 answers)
Closed 1 year ago.
What method should I use to convert 2021-03-20T00:19:07.000000Z to 2021-03-20 00:19:07, in PHP and Laravel 8.x, by the way, If you can explain what does the T in the middle of 2021-03-20T00:19:07.000000Z and the dot behind each represent? I will very much appreciate you !!
You can use Carbon date library in php and laravel.
Carbon::parse('2021-03-20T00:19:07.000000Z')->format('Y-m-d H:i:s')
Your input date is an ISO 8601 formatted date.
The T is the divider between date and time, so it is a static value.
To convert your ISO 8601 date to a YYYY-MM-DD hh:mm:ss date format, you can easily use DateTime (DateTime Documentation).
So in your case the solution would be:
$input = '2021-03-20T00:19:07.000000Z';
$datetime = new DateTime(input);
$output = $datetime->format('Y-m-d H:i:s');
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 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 an answer here:
Date in a URL dd/mm/yyyy
(1 answer)
Closed 5 years ago.
I am facing an issue in echo date() with some formats like:
if i use the format d/m/Y like: '18/04/2017' then date() does not recognize this because the format contain /
<?php
echo date("d-m-Y",strtotime('16/04/2017'));
?>
it will output: 01-01-1970
But when i use the format m/d/Y like: '04/18/2017' then date() it recognized
<?php
echo date("d-m-Y",strtotime('04/16/2017'));
?>
it will output: 16-04-2017
I am not getting these different behaviour of date(), can anybody help me please
Forward slash (/) signifies American M/D/Y formatting, a dash (-)
signifies European D-M-Y and a period (.) signifies ISO Y.M.D.
So if your date is like 04/16/2017, change the / to - and then use date() function on it to convert it to any format.
Reference
Working Code
You can use date_create_from_format to convert your date to any format you want. The problem with your code was that strtotime does not support this format.
Learn about strtotime formats
If you using dd/mm/yyyy then it will not work because does not comes under support format of strtotime. For using
Supported formats: (for dd mm yyyy)
dd [.\t-] mm [.-] YY
Example: 16-04-2017
Example: 16.04.2017
Example: 16\t04t\2017
<?php
ini_set('display_errors', 1);
$date=date_create_from_format("d/m/Y","16/04/2017");
echo $date->format("d-m-Y");
Another way is replace / with - of given date
$date = '16/04/2017';
$date = str_replace("/", "-", $date);
echo date("d-m-Y",strtotime($date));
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
hi have my : regional format date like : 01-Iulie-2014 or 01-Decembrie-2015 and I want to convert it into: 2014-07-01 or 2015-12-01. I tried something like this,but with no result:
$mydate = date('Y-m-d', strtotime($this->input->post('pay_date')));
but strtotime returns me false
Is there a function to do this stuff ?
strtotime() parses about any English textual datetime description into a Unix timestamp, so you could do, create an array of month names in your language and its equivalent english month name, and then use strtotime, as:
function custom_strtotime($your_date) {
//create month names as your_lang_month_name => english_month_name
$months_arr = array('Janvier'=>'jan',...,'Decembrie'=>'dec')
return strtotime(strtr(strtolower($your_date), $months_arr));
}
echo custom_strtotime($some_date);
From http://php.net/manual/en/function.date.php:
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().