I notice that MySQL save date as 0000-00-00 format. But in my application I have validate date for this 00-00-0000 format. I notice that date is not saving properly as different formats. Can I change mysql data format to 00-00-0000 format?
The 00-00-0000 format is not entirely clear; it could be dd-mm-yyyy for instance.
You can simply convert the date you have like so:
$mydate = '24-12-2012';
// create date object by using a known format
// see manual page for all format specifiers
$d = DateTime::createFromFormat('d-m-Y', $mydate);
// format date object in yyyy-mm-dd
echo $d->format('Y-m-d');
See also: DateTime::createFromFormat() DateTime::format()
You can also use MySQL str_to_date function
INSERT INTO yourtable (datefield) VALUES (str_to_date('01-02-2007', '%d-%m-%Y'));
As #Jack explained it is one of the correct way you can choose and
at the time of select date from database you can use
DATE_FORMAT(date,format).
You can't.
Instead of that you have to convert your custom format to database format and back.
To store it in the database you can use explode PHP function.
To when selecting, you can format mysql date format using DATE_FORMAT mysql function.
Related
I insert a date in PHP created like that :
$myDate = date('Y-m-d H:i:s');
It works perfectly. If I change the format like that :
$myDate = date('d-m-Y H:i:s');
The value inserted is 0000-00-00...
It exsists a way to insert a date in French format ?
MYSQL only allows yyyy-mm-dd format for storage so it is impossible to store dates in the French format.
You can change the field type to be a char or any other string type if you want to store the dates in any format of your choice but you won't be able to perform MYSQL date functions, during queries, on the field.
It is better to follow the standard format and have a script convert the date to whatever format you want when you want to display the date to users.
strtotime($_SESSION['starttime'])
I tried to convert the mysql time stored as $_SESSION['starttime'] of value 1422094831 but I want to convert the resulting to time using strtotime but returns no value. What could be wrong?
strtotime() is used to create a timestamp, not convert it to a date. If your session variable stores a timestamp, try using the date() function to convert it to a more readable format.
echo date("Y-m-d", $yourTimestamp); //Will print YYYY-MM-DD format date
The PHP doc will help you using the date() function correctly.
If that's not what you want to do then please provide more information in your question, from what I understand you already have a timestamp.
Hope somebody can advise on the following:
I have created a table into my database which has a coloumn with datatype defined as DATE. The normal MySQL date order is YYYY-MM-DD, however I need to enter it in format DD-MM-YYYY.
I am using php to insert data into my table, and I was wondering at what point should I convert the date format.
To summerize, I am asking the following:
Is it possible to insert into MySQL database where field is formatted as DATE, a date in format DD-MM-YYYY instead in format YYYY-MM-DD?
If not possible, can I convert a value posted from PHP in format DD-MM-YYYY into the format YYYY-MM-DD using the MySQL syntax or I should use PHP to post the value in YYYY-MM-DD format?
Thank you in advance
You would use STR_TO_DATE in your INSERT to convert the date to the ANSI standard date format.
INSERT INTO `table` (`date`) VALUES (STR_TO_DATE('01-05-2013','%d-%m-%Y'));
You can then use DATE_FORMAT to convert the ANSI standard date to the format you want.
A date is always stored in the same manner in the DB.
When inserting you have to give the DB a format that it can parse as date. And when reading from the DB you can format the date in any way you like.
So you have to format your data before inserting into the correct format. Otherwise the DB can't store it as date.
You can go to different mysql website and get this answer. For Example:
http://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
I'm building a website with php and i'm using the DATE-type in my MYSQL table to store dates. The problem that i have is that this stores the dates by default in the format YYYY-MM-DD. But i need this format DD-MM-YYYY to appear on my PHP page with the possibility of calculating the amount of days between 2 different dates. How can this be achieved?
That's a display problem. Use mysql's date_format() function to convert to whatever your display requirements are, e.g.
SELECT date_format(datefield, '%d-%m-%Y'), ...
You can use strtotime to convert a string representation of the date to an actual date object in php, then use the date function to spit out the date as any string format you wish. Also, you can be strtotime to perform date calculations. Additional information can be found at this blog post.
$phpDate = strtotime($stringDateFromDb);
date('d-m-y', $dateFromDb);
strtotime('-3 days', strtotime($stringDateFromDb));
Here is an example for a way to do it:
$date_str = '2012-05-20'; //you get it from db
$date_now = strtotime($date_str); //convert it to unix timestamp
$yesterday=$date_now-24*60*60; //make calculations
echo 'yesterday was: '. date('d-m-Y',$yesterday); //date() returns the date in format you need
Further example here: How to calculate the difference between two dates using PHP?
I have a MYSQL table column of type timestamp and using CURRENT_TIMESTAMP as default when a new record is added.
The date field in my database table looks like this 2011-08-16 12:09:25 How is it possible to format that into MM/DD/YY to display on my site? I tried some functions from the date helper but I get errors.
note: I'm trying to figure out how to use Codeigniter functions for this, if possible.
In the CI manual there are examples with $timestamp = '1140153693'; but my database timestamp is a different format and get errors.
To display a timestamp, just use the PHP Date class. But since MySQL outputs timestamps as a string not an INT, you'll have to first convert the timestamp string to a timestamp INT using PHP's strtotime function. The code looks like this:
echo date("m/d/y",strtotime($timestamp));
Date
http://www.php.net/manual/en/function.date.php
StrToTime
http://php.net/manual/en/function.strtotime.php
Sorry, I didn't properly explain the issue but solved it.
In order to use the Codeigniter date functions I had to convert the mysql ISO 8601 date format to a 32-bit integer.
I had to do the following
$unix = mysql_to_unix($row->message_date); //conversion to string
$datestring = "%m/%d/%Y";
echo mdate($datestring, $unix);
I was using the ISO 8601 format and getting errors.