From HTML date input to MySQL date format via PHP [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have some problems converting an HTML date to a MySQL date.
I have a form with an input field (type="date"), which outputs a date in format 29/04/2015
Whenever I click on the Send button, I send the date via PHP to a database which has a column in DATE format (2015-04-29)
Unfortunately the result is that the date is always stored as 1970-01-01, which means the conversion didn't work out as expected.
My conversion code, right now, is:
#$entryDate = str_replace("/", "-", $_POST['entryDate']);
#$entryDate = date('Y-m-d', strtotime($entryDate));
Do you have any idea why the conversion is not working?
Thanks a lot for your help

HTML5 is not truly/fully supported in all current browsers.
But if you can step over that, the input method gives you a date formatted in "YYYY-MM-DD" format (regardless of the localization of the display order in the representation to the user in the browser), no need to convert it to use in mysql at all. But do validate it as a valid input, esp. given the lack of support in some browsers (and for once it's not just IE, Firefox also has issues with this one).
See also Is there any way to change input type="date" format?

Related

Converting YYYY-MM-DDTHH:mm for Google Calendar [duplicate]

This question already has answers here:
How to convert ISO8601 to Date format in php
(6 answers)
Closed 2 years ago.
I'm currently having trouble figuring out how to convert the current timestamp to one that works as a parameter for google calendar. I'm using PHP.
Right now the format is YYYY-MM-DDTHH:mm. The date I'm trying to convert is:
2020-03-11 10:00:00
So far I've tried using gmdate and passing in the current date with strtotime but I can't figure out how to make it the right format.
Thanks for any help.
you can do in php like:
date('your needed format here', strtotime($yourtime))
check https://www.php.net/manual/en/function.date.php for format detail. that will give you the needed string.
date() will convert unix time format in given format, the strtotime() makes unix time out of almost any time format string, it will detect the format automatically.

Date input in HTML + PHP Validation

I am making a registration form and I'd like to get the birth date of each registered user.
<input class="field" type="date" name="birthday" max="9999-12-31" />
When I was making this, though, I realised that everyone can open the Inspect Element options and put any value they like. This will go to the database and just ruin the experience. I don't want to let this happen, so I am now looking for a way to do the following:
Find a way to check if the date is valid (no matter what the format is, mm/dd/yyyy and dd/mm/yyyy will both get accepted into the database but they will be converted to yyyy-mm-dd before going there)
Make sure the region doesn't matter (like I said, every format will get accepted but I'm a bit worried about the fact that some countries show the input field differently (For example, I stumbled across the дд/мм/гггг г. format which basically means dd/mm/yyyy y. Would that "y." at the end affect what is being stored as a variable in PHP? It just sits there, it's an abbreviation, it can't be changed by the user)
Not let the person enter a date higher than the current one. I'm thinking of adding a simple 13-years-old age limit. How can that be done?
Before asking this question, I have been searching for a solution to all these problems for a while now. And I found out about the checkdate() function but from what I learnt, it accepts only the mm/dd/yyyy format which is not something I want. And it also works a little bit differently. I need to input three separate values for day, month and year. So, I went to look for alternative solutions. I stumbled across this topic: Correctly determine if date string is a valid date in that format. The solution seems to be what I'm looking for. The only problem is that it only accepts the "yyyy/mm/dd" format. So, logically, I just have to find a way to detect what the format is (where my only concern is: what if the user opens Inspect Element and changes the input type... maybe the solution to this is a simple check if $_POST['birthday'] is a type="data" element first? I'm also still worried about that "г./y." which appears in some regions though). After the detection, the correct function for checking whether the date is valid or not will get iterated. After all of this, the date will be simply converted to yyyy/mm/dd and the website will make one last check to see if the user is at least 13-years-old and allowed to be registered. I don't get to work with PHP very often, so any kind of help is appreciated!
You should be able to use the PHP Date function by adding your input as string. Something like this:
$dob = date('Y-m-d', strtotime($_POST['birthday']));
This should take any date value input into the form an convert it to the format you want.
Then you could use date_diff to get the age in years and run a simple if statement:
$age = date_diff(date_create($dob), date_create('now'))->y;
if ($age <= 13)
{
...
}
Edit
It should also be able to convert any extras correctly:
$dob = date('Y-m-d', strtotime('02/01/2019 y'));
outputs: 2019-02-01
Edit 2
Based off of the accepted answer's edit here:
Looks like you can replace the '/' in a date with '-' and it should work:
$birthday = 15/05/1980;
$date = str_replace('/', '-', $birthday);
$dob = date('Y-m-d', strtotime($date));
This worked on my local server. Hopefully it helps! It also worked with the other date formats previously discussed.

html5 date field input is correct but output is reversed

if have the following problem. in my html5 datefield, i have the input order dd.mm.yyyy (for example todays date: 27.04.2017).
<input type="date" />
thats correct in my country and timezone. the posted value is in reversed order to my input. its yyyy-mm-dd (for example todays date: 2017-04-27).
is there any way to change the timeformat if the the value is posted?
ive found several solutions but only for the input and not the posted values.
I'm not really sure what you mean but here is something you can try
$time = strtotime($_POST['dateInput']);
if ($time != false) {
$mydate = date('d-m-Y', $time));
}
is there any way to change the timeformat if the the value is posted?
The field submits using a standard format. This is consistent across browsers that support the date input type and is required by the standard.
The user interface, on the other hand, is not consistent across browsers.
You can reliably expect a date field to always submit in the form YYYY-MM-DD so you can write a parser for it in your server side code (and then format it however you like).
Solve this problem on the server.
(You /could/ use JavaScript to parse the date, format it, and write it to a hidden input every time the date input changes … but doing it server side is simpler).

Converting client side time to UTC on the server for storage [duplicate]

This question already has answers here:
how to convert php date formats to GMT and vice versa?
(3 answers)
Closed 9 years ago.
This may be a duplicate but I can't seem to find what I'm looking for.
I want to store dates given by the client in UTC. I have a javascript library that detects the client's timezone and I'm sending the timezone name (e.g. America/Halifax) to the server with the rest of the form data.
I've been searching now for the php functions that I can use to take the date entered and the timezone and convert it to UTC.
I think gmdate is the correct function but how do I use the date entered and the timezone with gmdate?
Try setting date_default_timezone_set('America/Halifax') at the top of your script.
http://php.net/manual/en/timezones.php

Zend Date MySQL Timezone offset problem

I've got a site which uses the Zend Framework. There's a form which the users fill in, including a Date field. Currently I'm using this to create a new Zend_Date object and then getting the date in ISO format to put into the MySQL database. However when the date is returned in ISO format it also has the timezone offset appended to the end (e.g. 2011-01-01T00:00:00-0500), which MySQL doesn't like. When I try to add it to the DB it gives me an invalid date error. I'm sure there must be a simple solution to return the date without the timezone offset, but I can't seem to find it.
Any suggestions?
Thanks.
if you toString your Zend_Date object and then use the mb_strcut() function to remove the timezone and then insert it into the db it should be fine ?
another way would be to alter the Zend function that returns the date in an ISO format to prevent it from appending the timezone to the end of the date.

Categories