uk date format in mysql - php

i'm having trouble getting the date to be imported into mysql from my form.
my form is validated so that the input will always be dd/mm/yyyy
i'm currently using
$date = date('Y/m/d', strtotime($_POST['night_attending']));
this takes the value from the form and assigns it the year/month/day
the problem is that its reading my form as mm/dd/yyyy
i can swap the code to 'Y/d/m' which will put it into my database the right way round but it will stop working if the day is past the 12th as it still believes that it is the month
i have tried using
date_default_timezone_set('Europe/Dublin');
and
date_default_timezone_set('Europe/London');
but it makes no difference
i'm contemplating just using the mm/dd/yyyy format on my form, but this isn't great as it's a uk site.
has anybody encountered this problem? i'm sure it must be comman and there must be a simple answer that i'm missing.
thanks
alsweet

There is no means to set MySQL's date format - the options exist, but they aren't enforced/used.
I don't recommend storing the dates as VARCHAR in order to maintain your format -- use the MySQL format, and work with MySQL date functions (IE: STR_TO_DATE, DATE_FORMAT) to output the format you want for screen.
Be aware that dates will be dependent on the timezone of the host MySQL is on - you might want to consider using epoch timestamps instead, depending on your needs.

This will give you the mysql date format from UK date.
$timestamp = strtotime(str_replace('/', '.', '03/04/2011'));
$mysql_date = date('Y-m-d', $timestamp); // 2011-04-03

You can use strptime() to parse the date:
$dateArray = strptime('%m/%d/%Y');
or date_parse_from_format() on PHP 5.3.
EDIT: I saw this too in the comments for strtotime, might help.

Related

How to convert two different strings to date and store it in single column in MySQL database?

Currently I'm working on PHP and MySQL Project. I'm taking DATE INPUT from users in string format. So user can enter any format in text-box. For e.g. dd/mm/yyyy or dd-mm-yyyy or yyyy-mm-dd or mm/dd/yyyy etc. (I know its too bad practice). But my question is:
is it possible to convert these all in single format and store it in
single column?
.
For converting I'm using str_to_date() function, but it accepts only one format to convert. How can I add other formats to convert string to date ?
try this, should work:
$mysqlFormatedDate = date('Y-m-d H:i:s', strtotime($yourTime));
PHP will take $yourTime and resolve it's format, then it will convert it to unix timestamp, and then it will convert to mysql datetime format which is Y-m-d H:i:s then you just need to save it to your DB.
you can do this in your mysql_query using STR_TO_DATE:
STR_TO_DATE($yourTime, '%c/%e/%Y %r')
You can use "RLIKE()" to check fo some patterns but that will fail because you can't differentiate between international and retarded. Imagine someone typing in "12/11/2018". Is it the 11th of December or 12th of November? Also you wrote "e.g.", so if you allow everything, what would you make of "20122007"? 20th December 2007, 20th July 2012?
Use a JS-Library for date inputs or do inputs for day, month and year separately. Otherwise it's pretty hopeless.

Insert date in specific format into MySQL db

I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y
As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.
I tried to insert the date using the following functions:
1) date('d-m-Y');
2) (new \DateTime())->format('Y-m-d');
I understand these two functions do pretty much the same thing but I was not sure what else I should try.
MySQL threw the following error for both dates:
Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1
I am guessing this should be an easy fix but I can't figure out what is wrong.
I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).
Any suggestion is welcome!
$newdate= date('Y-m-d', strtotime('10-09-2015'));
or if you want current time just use
$now = date('Y-m-d');
If your msg_date column's structure is DATETIME or TIMESTAMP, the date format should be:
YYYY-MM-DD HH:MM:SS
which can be formatted through PHP like this:
$date = date("Y-m-d H:i:s");
Or if you already have a date, and you want it to convert to that format, we can use strtotime():
$date = date("Y-m-d H:i:s", strtotime($date));
For more date format, check this link.
The MySQL error message indicated that you had the date format the wrong way around.
Year must go first, then month, then day, as in:
date('Y-m-d') // right
In your first example, you have
date('d-m-Y') // wrong
In one of your examples above, you have it right, but you say you got the same response, so I assume that was not what you actually tried.
Another thing to note is that a MySQL TIMESTAMP column stores both a date and time. It's valid to give MySQL just a date (MySQL will just leave the time at zero), but if you have no need to store a time, you may as well make the column DATE instead of TIMESTAMP.
If you want to display your dates as d-m-Y then by all means do so, but they need to be sent to MySQL as Y-m-d.

how to echo out 12 hour time format in my page

please i need your help i created a comment box in my page, stored inside phpmyadmin with time type DATETIME. the problem am having is the time always display in 24 hour format and i want it to display in 12 hour format (PM/AM) and be stored inside mysql. i have tried using date() function at the same time i used date("y-m-d H:i:s") instead of now())function but the result i keep on getting is in 24 hour format
see the code
$insert = mysql_query("INSERT INTO test (name,comment,whenadded) VALUE ('$name','$comment', now())");
With this code i get the result in 24 hour time format.
whenadded is the DATETIME variable name.
thank you in advance.
You want to store the date as DATETIME in the MySQL DB, thats good practice.
For output, use PHP's date() function. Look at this answer. Or you use MySQLs date_format() function.
SELECT date_format(whenadded, 'Y-m-d h:i') AS my_date FROM ...
The php documentation should help http://www.php.net/manual/en/function.date.php
what you are looking of is date(y-m-d g:i a) wich will give something like "2013-12-30 4:38 pm"
Let the mysql decide its date format, it's mostly irrelevant for you.
What you need, is to properly format your output data, like:
echo date("y-m-d h:i:s A", strtotime($date));
Where $date is the variable you get from MySQL.
In no particular order:
phpMyAdmin is not a database engine. MySQL is.
Dates are not stored in any particular format. You give format when you convert them to strings.
The mysql_... legacy extension is deprecated, insecure, triggers a notice in latest PHP versions and will be removed.
Your code is probably vulnerable to SQL Injection.
The H format code means: 24-hour format of an hour with leading zeros.
It's good to save the data within 24Hours Format, but you can show it within 12Hours plus am/pm
date("d/m/Y - g:i A");

php date to mysql returns 0's

Hello i am trying to add a date time from PHP into my MySQL database, in the database I have tried date time and time stamp and I have tried it with and without current_time set as the default option, in my php I have the following.
date_default_timezone_set("Europe/London");
$date = date('m/d/Y h:i:s a');
My hope was to just add the $date into the value's part of the upload query however in all cases it comes back as 0's, it echo's the date fine, however it uplaod's 0's, any help is appreciated, Thanks.
'm/d/Y h:i:s a is not a standard MySQL datetime format so unless you are storing it as varchar/char you will get the results you are seeing.
Your options are to:
Store the date in a standard format (datetime Y-m-d H:i:s, timestamp) and convert it to whatever format you want during the query (recommended)
Store it as a string but lose all of the datetime functionality MySQL offers. Losing this functionality will make working with dates in your queries and PHP very painful and is not recommended to do.

PHP datetime from string

After spending over 6 hours trying to do this and trying many different published solutions I am just going to ask the exact question.
I want to have the user enter the date and time in US format in an html form. Format for today is 12/16/2012 02:53 using 24 hour time format.
Lets call it start_date.
Then I want to insert the record including the start_date into an mysql database into a datetime type field.
I am using PHP 5.2. Many of the solutions I saw required 5.3 and none of the workarounds for 5.2 worked.
Can someone please give me an exact example.
Thank you.
Use regex or string processing to extract fields from your current format.
Create date in MySQL format.
Insert in the database.
See here : date_create_from_format equivalent for PHP 5.2 (or lower)
Actually the format of your date in not valid to be inserted in mysql table the format must be YYYY-mm-dd Hour:min:sec, in order to be place in datetime field. But if you use the field type as varchar you don't need to care about format. you can insert in whatever format you wish.
Or you can rely on MySQL parsing:
SELECT STR_TO_DATE('12/16/2012 02:53', '%m/%d/%Y %H:%i')
Note: this expects two-digit month, day and hour, i.e. 01 - not 1.
See MySQL Date format for other formats.
Also for this approach to be of practical use you will have to process failed parsing attempts: for example, you can make your Datetime column NOT NULL so that all inserts or updates fail if you tried to write NULL into it (STR_TO_DATE will return NULL for invalid date)
You asked for an example, which no one has supplied yet, so here it is:-
$start_date = date("Y-m-d H:i:s", strtotime("12/16/2012 02:53"));
echo $start_date;
Output:-
2012-12-16 02:53:00
This format matches the MySql DateTime type.
See working example here which also demonstrates that it works in PHP 5.2.
See the manual for strtotime and date.
You can use strtotime(). It parses dates according to the format. For instance dates using / (MM/DD/YYYY) are parsed using the American format, and dates using - or . (DD-MM-YYYY or DD.MM.YYYY) are parsed using the European format. See the third note in the documentation.
You really should look at upgrading to 5.4 if at all possible. There you can use the really nice date classes.

Categories