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.
Related
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.
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");
While creating a table, I defined one column of DATE type and one of TIME type. As I insert the values using a php script like :
date--> 2013-11-11
time--> 12:12:12
and when I query the sql browser I see those values in exactly the same manner. But I am unaware of the format with which it stores the date and time. Like yyyy-mm-dd or yyyy-dd-mm.
Is there any way I change it ?
Dates and times are stored in MySQL in the format "YYYY-MM-DD" and "YYYY-MM-DD HH:MM:SS" which is not necessarily the format you want to display in your web page or application. There are two methods to reformat the date and time into the desired format. One is to do it in the SQL query in MySQL calling the DATE_FORMAT() function and the other is to do it with the programming language retrieving the data from the MySQL database.
From MySQL 5.1:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
For second question: you can't change default DATE format for the storage, please see this question also
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format
http://dev.mysql.com/doc/refman/5.5/en/time.html
MySQL retrieves and displays TIME values in 'HH:MM:SS' format
I do not believe this can be changed. But you do not care. You can extract dates and times in the format of your liking with the DATE_FORMAT() and the TIME_FORMAT() functions.
If you want to know the internal storage of Date columns, you can check Date and Time Data Type Representation, but I think you want to select date in different format; which other guys already answered about it.
It is stored in 3 bytes, and it is always YYYY-MM-DD.
The datetime is in Y-m-d H:i:s format, or year month day and hour minute second. If you only use a part, the format stays the same.
If you want to change the format there are many ways. The easiest would be to do something like return date("Y-d-m H:i:s", strtotime($mysqldatetime)); (will turn it to dutch date);
Keep in mind that you are using two seperate columns, one for time and one for the date. If you use only one column the missing values are filled with default values (time would be 00:00:00 and date would be 1970-01-01
In my php application I have this code:
<?php echo date("d/m/ Y ",strtotime($row["m_date"]));?>
In it, $row["m_date"] is fetching from a database.
The problem is that all the dates are printing perfectly except 27/2/2011. It's printing 1/1/1970 instead.
The date in the database is fine, and prints correctly in a PDF.
I'll assume you're getting the date from the database as the string 27/2/2011 because that's most probably what happens (correct me if I'm wrong).
PHP considers the string 27/2/2011 as being in the m/d/Y format, not d/m/Y and tries to parse under that assumption. Because the date is not valid under that format strtotime returns false. Giving false as the timestamp parameter to date is taken as 0, which is the timestamp for January 1st 1970.
What you need to do is either get your date in another format (or better still, as a timestamp) from the database, or parse it yourself (say using explode).
Good luck,
Alin
The database should be able to return the date to you as a UNIX timestamp. For example, MySQL has the UNIX_TIMESTAMP() function.
SELECT UNIX_TIMESTAMP(date_column) FROM table;
Postgres has date_part
SELECT DATE_PART('epoch', date_column) FROM table;
Most other databases should have similar features. If you can get the date out as a UNIX time stamp you can pass that directly to date() without having to use strtotime() as well.
All of this does of course assume you're using a temporal datatype for the columns in question (timestamp, datetime, timestamp with time zone, etc) and not just storing a string. You are using a temporal type, right? If not, then why not?
if you are storing the date in the database as a timestamp this should work
<?php echo date("d/m/Y",$row["m_date"]);?>
if you are storing the date in the database as a date or datetime this should work
<?php echo date("d/m/Y",strtotime($row["m_date"]));?>
How is the m_date stored in the databases? Is it a datetime object? Or a string.
Problem with strtotime is that it isn't real good at deciphering written dates. So something like 27/2/2011 gives problems while 27/02/2011 gives no problems at all.
So there are 2 solutions:
Make sure all the dates that get entered into the database are of the correct format (dd/mm/yyyy).
Write a regular expression that adds a leading zero to all single characters.
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.