This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
strtotime failing on mm-dd-yyyy hh:mm
I have a date coming in like shown below but the DateTime function will not allow it:
$start = new DateTime('13/10/2012');
echo $start->format("Y-m-d");
It allows all other methods, even using '10/13/2012' but I can't change it because that's the format i'm receiving the date in.
Any ideas?
Thanks
Did you try, createFromFormat() ?
$start = DateTime::createFromFormat('d/m/Y', '13/10/2012');
try DateTime::createFromFormat like
$start = DateTime::createFromFormat('d/m/Y', '13/10/2012');
According to the documentation, it only accepts dates in specific formats, one of them is "american month, day, year", which will obviously fail if you provide 13 as the month.
I always try to avoid working with dates in ambiguous formats. Mostly using "2012-10-13" within the code, and "13 Oct, 2012" for user-facing dates. Both can be parsed reliably by any date formatting API I've got experience with.
But when forced to work with an ambiguous format, I parse it manually instead of relying on PHP's built in API, since I fully understand what it's going to do. For example:
list($day, $month, $year) = preg_split('/[^0-9]+/', '13/10/2012');
$start = new DateTime("$year-$month-$day");
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
As ive echoed out the datetime value from my db, i am now trying to display this value to edit it in the datetime-local field within my form.
The datetime vaue from db is set to:
22/3/2017 10:00:00
however, after attempting to use the following code, im left with this:
1970-01-01T01:00:00
$dat = date("Y-m-d\TH:i:s", strtotime($_GET["dat"]));
How & why is this function not working correctly to display '22/3/2017 10:00' in the form field?
Use DateTime::createFromFormat:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/3/2017 10:00:00');
$dat = $date->format('Y-m-d\TH:i:s');
echo $dat;
Your code is not working because strtotime makes assumption based on delimiters about actual format:
m/d/Y- American format
d.m.Y or d-m-Y - European
It's not working because strtotime() can translate only specific date format.
Check the manual. For a list of supported date format, look here.
Your date format looks not included in the supported ones to me.
Examples:
strtotime("03/22/2017 10:00:00"); // Works: returns 1490173200
strtotime("22/3/2017 10:00"); // Doesn't work: returns false
You have to either change the date format in your DB or format it to one of the supported formats to make it work.
I'm facing an issue with managinging dates, some dates pass others dont. I want to produce an insertable date for mysql. there are two possible types of post dates
yyyy-mm-dd //should go without conversion
m/d/yyyy // should be converted
I'm using this
$date = $_REQUEST['date'];
$date_regex = '/(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/';
if(preg_match($date_regex, $date)){
$date = DateTime::createFromFormat('m/d/Y',$date)->format('Y-m-d');}
problems
I realised this regex is failing for dates like
2/5/2013
but has been working for
12/12/2013
so I removed it BUT still
DateTime::createFromFormat('m/d/Y',$date)->format('Y-m-d');
is also failing for m/d/yyyy
This date thing has got my head spinning for the last 6 hours.
In this case, there is no need to use DateTime::createFromFormat because m/d/yyyy is one of the recognized date formats (see "American month, day and year"). Just convert it to a DateTime object and let the constructor handle the format and forget the regex:
$date = $_REQUEST['date'];
$datetime = new DateTime($date);
$datex = $datetime->format('Y-m-d');
The reason DateTime::createFromFormat('m/d/Y',$date) fails for dates like 2/5/2013 is because you are forcing it to be specifically 'm/d/Y' and that date does not fit that pattern. You can see a list of all date formats here. Specifically, m expects there to be a leading zero (like 02), so when you give it one without that, it won't recognize it. Same goes for d. In this case you would have to use n and j respectively. But, like I said, let the constructor do the hard work for you.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: Date larger than current date
I have my dates in this format: dd-mm-yyyy
Now according to php, 28-06-2011 is bigger than 01-11-2011
$today = date('d-m-Y', time());
if("28-06-2011" > $today ){
echo "This returns true";
}
How can i make this work properly?
You can't compare dates like that. They're two strings. PHP will try to interpret them as numbers (taking the first two digits and then failing because of the -) and compare those. Turns out this is not correct. YYYY-MM-DD type strings can indeed be compared using > and <.
So refer to Col. Shrapnel's answer and change your date format - it makes sense anyway. If you can't do that (because it's a user-input date or whatever), you can create two DateTime objects which, thanks to PHP 5 magic, you can also compare like this:
$date1 = new DateTime('2000-01-01');
$date2 = new DateTime('2001-01-01');
if ($date1 > $date2)... // this works
if(strtotime("28-06-2011") > time()) {
}
I have my dates in this format: dd-mm-yyyy
That is where you are wrong.
you shouldn't have your dates in this firmat. Have them in yyyy-mm-dd and you'll be able to compare them with no problem.
You cant compare date with string.
Create another date item similar to today variable with fixed date of 28-06-2011 and then compare 2 objects.
$datetocompare = date("d-m-Y", mktime(0, 0, 0, 28, 06, 2011));
if($datetocompare>$today) {}
this would then work.
Source: http://uk.php.net/manual/en/function.date.php
If you do it that way, you're actually comparing strings, rather than dates. To correctly compare dates, you would need to convert the date values/strings to time stamps, you can use the strtotime function to accomplish just that.
Here is a link with more info: http://php.net/manual/en/function.strtotime.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert one date format into another in PHP
I want to convert the date format from a particular format to some other.
For example,
Convert the date from ( 2011-06-21 or 2011/06/21 or 06/21/2011 ) to 21062011.
Is there any function to do this job.
Thanks in advance.
var_dump(
date('dmY', strtotime('2011-06-21')),
date('dmY', strtotime('2011/06/21')),
date('dmY', strtotime('06/21/2011'))
);
You should use the DateTime class.
$date = new DateTime('2011-06-21');
echo $date->format('dmY');
It can be used procedurally, if you wish.
var_dump(
date_format(date_create('2011-06-21'), 'dmY'),
date_format(date_create('2011/06/21'), 'dmY'),
date_format(date_create('06/21/2011'), 'dmY')
);
Hi you should be able to to use date combined with strtotime like so
$date; //the date that needs to be formatted
<?php date('dmY', strtotime($date)); ?>
So inside the date() function you simply format the date however you want the original date to be
php strtotime
Hope that helps
Unfortunately dates are very locale specific. strtotime() does not observe all of the niceties of the locale (and date_parse is a simple wrapper around strtotime). e.g. today is 21/06/2011 in the UK, and 06/21/2011 in the US.
A more robust solution is to use the DateTime class and its createFromFormat method.
However, IME, unless you are sourcing the input data from a consistent machine generated source, a better solution is to use a tool which facilitates input in a consistent format
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');