I need to convert mysql datetime to php Y-m-d. Essentially I just need to drop the hh:mm:ss from the datetime. How can I do this?
Furthermore, once I have the PHP dates in the format Y-m-d how can I do a comparison of two dates to tell which is higher? For example if I have 12-31-13 and 1-1-14 can I just do if (12-31-13 < 1-1-14)?
$formattedDate = date("Y-m-d", strtotime($mysqlDate));
if (strtotime($somedate) >strtotime($someotherdate)){
// do stuff here
}
strtotime standardizes most dates and times into the number of seconds since jan1 1970.
I need to convert mysql datetime to php Y-m-d. Essentially I just need to drop the hh:mm:ss from the datetime. How can I do this?
Use MySQL's DATE() function
Furthermore, once I have the PHP dates in the format Y-m-d how can I do a comparison of two dates to tell which is higher? For example if I have 12-31-13 and 1-1-14 can I just do if (12-31-13 < 1-1-14)?
You can compare DateTime objects. (Notice the formats of the dates)
$date1 = new DateTime('13-12-31');
$date2 = new DateTime('14-01-01');
if ($date1 < $date2)
{
// do stuff
}
for comparing date use strtotime, and date u can find plenty of examples.
$orig = "2000-01-01";
$new = date("d-m-Y", strtotime($orig));
after thad you can compare dates like $orig > $new
Related
I'm trying to get today's date, and compare it to the date in my database, but the date in my database returns in a different form from the date that I get from the date function, so if I compare them in an if statement, the values are always going to be false. Is there a way for me to compare them so that it returns as true?
$date = date('y-m-d'); //date from date function ---> 15/07/19
$dateFromDatabase; //date from database ---> 2015/07/19
if ($date == $dateFromDatabase) {
echo "It's the same day.";
}
strtotime
You have in php some great functions to convert your human readable dates to timestamps.
The first magic function is called strtotime (string to time) : give it your date, you get a UNIX timestamp! Let's see some examples:
echo strtotime('2008-04-12 13:24');
echo strtotime('12 april 2008 13:24');
echo strtotime('12.04.2008 13:24');
And more powerfull, strtotime can recognize some keywords:
echo strtotime('now');
echo strtotime('+4 days');
echo strtotime('+1 month');
echo strtotime('next monday');
echo strtotime('+2 weeks 3 days 4 hours 23 minutes');
The second argument of strtotime is a timestamp, and its default value is the actual timestamp (time()). So echo strtotime('+4 days') is relative to the current time. Of course you can also give strtotime your mysql date! (Note you can also use the mysql function UNIX_TIMESTAMP, which use a bit more ressources).
To compare dates, it's now just a detail:
// your first date coming from a mysql database (date fields)
$dateA = '2008-03-01 13:34';
// your second date coming from a mysql database (date fields)
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// bla bla
}
Better than substring, isn't it ?!
Here is just another example, not relative to current date but to a particular date:
strtotime('23 hours ago', strtotime('2005-04-13 14:00'));
This mean 23 hours ago relatively to the second given date, which must be a timestamp.
user manual doesn't give a complete description of the supported date formats. Strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.
date in dd/mm/YYYY format, can be convert it to YYYY-mm-dd by using explode() function, but I think there are better solutions.
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));
I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date. If the date containing in a variable is greater than the today's date I want to echo "Error" and if it is less than or equal to today's date I want to echo "Success".
For this thing I don't want to use DateTime class.
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Following is the variable containing date in MM-D-YYYY format
$form_data['reg_date'] = '12-11-2014'; //This is today's date i.e. 11th December 2014
If the variable $form_data['reg_date'] contains date greater than today's date(i.e. 11th December 2014) it should give error message otherwise should echo success message.
Thanks.
I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date.
You cannot compare string representation of date in format m-d-Y. This format is invalid format, and php will not understand it. Read the manual what date formats are valid.
Best way to compare string dates is to have it in format Y-m-d or convert your string date to unix timestamp integer. But once you have date in Y-m-d format, it is trivial to convert it to unix timestamp, so converting it to timestamp just for comparing is an unnecessary step.
Convert m-d-Y to m/d/Y format, and then to unix timestamp:
$date = '12-25-2014';
$date = str_replace('-', '/', $date);
var_dump($date);
var_dump(strtotime($date));
if (strtotime($date) > strtotime('today')) echo "ERROR";
else echo "SUCCESS";
demo
But this I already explained in answer of your previous/same question (see 2nd link).
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Other method could be converting format with sscanf() function:
$date = '12-25-2014';
sscanf($date, "%d-%d-%d", $m, $d, $Y);
$date = "$Y-$m-$d";
var_dump($date);
if ($date > date('Y-m-d')) echo "ERROR";
else echo "SUCCESS";
demo
But I would still recommend you to use DateTime class, like I already explained if my previous answer.
Please try below code
if(strtotime(date('d-m-Y')) == strtotime($form_data['reg_date'])){
echo 'Today\'s Date';
}
First you have to convert your date in timestamp value like
$date_time = strtotime("11-12-2014");
and then you can do this to compare today date with the date you have.
$diff = $date_time - time();
This will gives you the date difference in seconds.
Given a date as a string like 'October 12, 2010'?
Should there be some Date "type" I should be converting to. Or is it just a matter of converting it to another string formatted as '2010-10-12' If so, what is the simplest way to convert to the yyyy-mm-dd format given my starting format?
Yes, DATE fields in MySQL just need to be a string in the correct format. To convert your date to the right format, use date combined with strtotime.
$date = 'October 12, 2010';
$sqlDate = date('Y-m-d', strtotime($date)); // 2010-10-12
Note: 'Y-m-d' is for a DATE field, if you're using DATETIME, use 'Y-m-d H:i:s' instead.
$date = new DateTime('October 12, 2010');
$sqldate = $date->format('Y-m-d');
DateTime is PHP 5 >= 5.2.0
You could also use strtotime() however that tends to break if you want to use a date in the far future (on 32b systems).
There are a lot of questions that ask about 'UNIX timestamp to MySQL time'. I needed the reversed way, yea... Any idea?
Use strtotime(..):
$timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);
Also check this out (to do it in MySQL way.)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
You can mysql's UNIX_TIMESTAMP function directly from your query, here is an example:
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Similarly, you can pass in the date/datetime field:
SELECT UNIX_TIMESTAMP(yourField);
From one of my other posts, getting a unixtimestamp:
$unixTimestamp = time();
Converting to mysql datetime format:
$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);
Getting some mysql timestamp:
$mysqlTimestamp = '2013-01-10 12:13:37';
Converting it to a unixtimestamp:
$unixTimestamp = strtotime('2010-05-17 19:13:37');
...comparing it with one or a range of times, to see if the user entered a realistic time:
if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}
Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:
if(ctype_digit($_GET["UpdateTimestamp"]))
{...}
$time_PHP = strtotime( $datetime_SQL );
Instead of strtotime you should use DateTime with PHP. You can also regard the timezone this way:
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $mysqltime, new DateTimeZone('Europe/Berlin'));
$unix_timestamp = $dt->getTimestamp();
$mysqltime is of type MySQL Datetime, e. g. 2018-02-26 07:53:00.
Slightly abbreviated could be...
echo date("Y-m-d H:i:s", strtotime($mysqltime));
I have two dates in the format below:
Start Date = 30-10-2009
End Date = 30-11-2009
How, with PHP could I calculate the seconds between these two dates?
Parse the two dates into Unix timestamps using strtotime, then get the difference:
$firstTime = strtotime("30-10-2009");
$secondTime = strtotime("30-11-2009");
$diff = $secondtime - $firstTime;
The function strtotime() will convert a date to a unix-style timestamp (in seconds). You should then be able to subtract the end date from the start date to get the difference.
$difference_secs = strtotime($end_date) - strtotime($start_date);
I'd rather advice to use built in DateTime object.
$firstTime = new DateTime("30-10-2009");
$diff = $firstTime->diff(new DateTime("30-11-2009"));
As for me it's more flexible and OOP oriented.
In fact previous answer will give you a DateInterval object but not seconds. In order to get seconds with OOP approach you should do this:
$date1 = new DateTime("30-10-2009");
$date2 = new DateTime("30-11-2009");
$seconds = $date2->getTimestamp() - $date1->getTimestamp();