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).
Related
How i can reproduce this date format with php?
2016-04-07T09:03:32
I'm using:
$date = date('Y-m-d h:i:s', time());
But i don't know what is The 'T' printed beetween date and time
What youre looking for is:
$date = date('Y-m-d\Th:i:s', time());
The 'T' is for HTML5 local date-time required format.
If you want to use this format to be sure of compatability, you should use the constant to speficy the format which includes the timezone offset between local and GMT.
$date = date(DateTime::ATOM, time());
which will output:
2016-04-07T01:20:48-07:00
The T designates the start of a time string in the ISO 8601 date format.
You can reproduce this format using date like this:
date('c')
But note this will (and should) also include the timezone offset. Because the date string will always be the same format you remove this quite easily to match your specified format with substr like so:
$date = date('c');
$formattedDate = substr($date, 0, 19);
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.
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
This is my date format:
d.m.y
but I need it like dd.mm.yy
How can with php check format and if is d.m.y then convert it to dd.mm.yy and if is already dd.mm.yy leave it.
If is y-m-d conver it to yy-mm-dd
How can i do with regex?
Why regex. Because my datum is CONSTANT. And for each installation is different. But for some cases must be not d.m.y but dd.mm.yy for example.
but can be some other format also.
$show_date = DateTime::createFromFormat('d.m.Y', $dateInput)->format('Y-m-d');
DateTime
You can use
$date = date('d.m.y', strtotime($your_date))
or
$date = new DateTime($your_date);
echo $date->format('d.m.y');
Try this :
$date = new DateTime('2000-01-01'); /// you need to convert you input date to this format
echo $date->format('d-m-y');
I need a function that takes any date/time and re formats it to a different format.
e.g.
$date = "31/12/2010 15:00:00" => has a format of d/m/Y H:i:s
so if i need a different format like
$returning_format = "m/d/Y H:i:s" => for America date/time
$newdate = transoform_date($current_format,$returning_format,$date);
the above line should return 12/31/2010 15:00:00
NOTE: I don't need it for this format only coz i can explode it and re-arrange it, so it can accept any format and return the desired format date.
For PHP 5.3+
$newdate = DateTime::createFromFormat($current_format,$date)->format($returning_fomrat);
strtotime — Parse about any English textual datetime description into a Unix timestamp
function transform_date($return_format, $date)
{
return date($return_format, strtotime($date));
}