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
Related
I want to parse the date 1938 1938+02:00 using date() & strtotime().
My code:
echo date("Y", strtotime("1938+02:00"));
gives me as result "2014"..
What am i doing wrong?
For something like this just get the first four characters of the string. No need to work with dates and such:
echo substr('1938+02:00', 0, 4);
Demo
But if you insist on using date functionality you'll need to use DateTime::createFromFormat() as that date string is not a standard format.
$date = DateTime::createFromFormat('YP', '1938+02:00');
echo $date->format('Y');
Demo
date("Y"); only return the year. That's what the Y does.
See the Manual page for date for other options.
EDIT
Another thing to consider, is that timestamps only go back to 1970. That's what a timestamp is (the number of seconds since 1970).
So, that's going to give you a negative value for the timestamp.
Your date string is not in an acceptable format. here is a list of acceptable formats for strtotime
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 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");
All I need is to compare dates represented by string values in the format d.m.y (e.g. 15.07.11 for today).
strtotime unfortunatelly gives me wrong results (probably because of the format). I found a answer to my question (PHP strtotime incorrect conversion) but DateTime::CreateFromFormat is not available in my PHP version. I could not find strfptime but think the autor of the answer meant strptime. The result of strptime is a array. I was surprised that I can compare arrays but the compare result is not valid.
What would be the easiest way to compare dates in the given string format with PHP <= 5.1.6?
You could always pass the result of strptime to mktime and get a usable Unix timestamp which you can compare or feed to the DateTime objects.
<?php
$d = strptime('15.07.11', '%d.%m.%y');
$timestamp = mktime($d['tm_hour'], $d['tm_min'], $d['tm_sec'], $d['tm_mon'], $d['tm_mday'], 1900 + $d['tm_year']);
echo date("j F Y", $timestamp);
?>
The only thing to watch for is that strptime gives the year as the number of years since 1900, and mk_time just takes a year number, so I added 1900 to it.
Duplicate
Managing date formats differences between PHP and MySQL
PHP/MySQL: Convert from YYYY-MM-DD to DD Month, YYYY?
Format DATETIME column using PHP after printing
date formatting in php
Dear All,
I have a PHP page where i wil be displaying some data from Mysql db.
I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002)
I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'
Can any one guide me
Thanks in advance
Use strtotime to convert the strings into a Unix timestamp, then use the date function to generate the correct output format.
Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:
$date1 = "28/04/2009";
$date2 = "28-04-2009";
function ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
$date1 = date($dateFormat, ukStrToTime($date1));
$date2 = date($dateFormat, ukStrToTime($date2));
You should be all set with this:
echo date($dateFormat, strtotime($date1));
echo date($dateFormat, strtotime($date2));
You may want to look into the strptime function. This can convert any date from a string back into numeric values. Unlike strtotime, it can be adapted to different formats, including those from different locales, and its output is not a UNIX timestamp, so it's capable of parsing dates before 1970 and after 2037. It may be a little bit more work though because it returns an associative array though.
Unfortunately it's not available on Windows systems either so it's not portable.
If for some reason strtotime will not work for you, could always just replace the offending punctuation with str_replace.
function dateFormat($date) {
$newDate = str_replace(/, -, $date);
echo $newDate;
}
echo dateFormat($date1);
echo dateFormat($date2);
I know this will make most folks cringe, but it may help you with formatting non-date strings in the future.
rookie i am. so came up with the method that just do that. what mysql needs.. shish i used param 2... hope it helps. regards
public function dateConvert($date,$param){
if($param==1){
list($day,$month,$year)=split('[/.-]',$date);
$date="$year-$month-$day"; //changed this line
return $date;
}
if ($param == 2){ //output conversion
list($day,$month,$year) = split('[/.]', $date);
$date = "$year-$day-$month";
return $date;
}
}