This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP mysql insert date format
I'm trying to insert a date in to my mysql datebase. The problem is that when I for example try to save 2013-01-18 it saves it like this: 000-00-00.
$date = date("Y-m-d");
mysql_query("insert into events values('','$event','$notes','$date')");
What should I do to fix this?
It's better to convert it to Unix timestamp first, and then save the timestamp. It's a lot easier to sort etc.
As for your question: if you really want to save it that way, your column should be a 'varchar'.
Also, mysql_query is deprecated. Don't use that anymore, it won't be supported for long anymore.
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a database that I receive every month that I simply import through CSV into MYSQL. I don't worry about preserving any old database entries, so I just overwrite the old database with the new entries monthly.
I want to be able to sort through the database by month, however I currently need to import the date field as VARCHAR, or risk having to manually convert tons of entries to fit the formatting of the MYSQL date field.
I would like to take the entries stored in the 'Expiration Date' field and be able to run a query that will convert them to a MYSQL date format and then write them to a new column named 'Expiration_Converted' so that I can then run date functions to show me all dates from a month, year, etc.
I know about the STR_TO_DATE function in MYSQL, however I'm not quite sure how to pull this off. I'm writing everything in PHP.
Any advice would be greatly appreciated.
Thanks!
Try this one.
I'm still using this script in my web application when I get the value from a text field and need to converto to MySQL DATE Format:
$format = '/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/';
if ($_date != null && preg_match($format, $_date, $parts)) {
$date = date_create($parts[3].'-'.$parts[2].'-'.$parts[1]);
$final_formated = date_format($date, "Y-m-d");
return $final_formated;
}
PS: $_date variable receives a date string.
After running this script, I can store date in Database.
Hope it helps you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use DATE FORMAT in my query?
I have a date from form post as 09-23-2012 and in my sql database i save date with datetime format like 2012-09-23 23:11:13
I want to create a query to compare these two date.
These two date should equal.
Try DATE_FORMAT:
WHERE DATE_FORMAT(the_field, '%Y-%m-%d') = CAST(:yourInput AS DATE)
Or something similar
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Inserting mm/dd/yyyy format dates in MySQL
Users are filling out a form for birthdate in this format MM-DD-YYYY.
I want to insert it into My SQL database in SQL Date format, YYYY-MM-DD.
What is the easiest way to change it? I considered doing a MID function, but was hoping to use something more efficient.
Thanks in advance!
***Yes this was answered before, but I was unable to find it, Thanks for the link!
Try this:
$sql = "INSERT INTO `my_database` (`my_date_field`) VALUES (STR_TO_DATE('".$my_date."', '%Y-%m-%d'));";
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
PHP - Convert to date format dd/mm/yyyy
mysql saves my date and times like this...
2011-08-31 08:48:40
And when I echo them from the database this is how they appear, how can I get them to a universal format like m/d/y...
Or does it not matter if the mysql row is not a DATE_TIME and just VARCHAR and insert it into the database how I want it??
Thanks...
Do it in PHP:
date("m/d/Y", strtotime('2011-08-31 08:48:40'));
Or do it in MySQL:
SELECT DATE_FORMAT('2011-08-31 08:48:40','%m/%d/%Y')
Of course you won't use hardcoded values in your code. You'll use the column name for MySQL or the retrieved column value, that's probably an element in an array, in PHP.
This post is previously answered:
Convert to date format dd/mm/yyyy
Regards
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 10 years ago.
I am newbe to php. I have small problem to compare dates in php.
I have 4 dates named date1, date2, start_date and end_date in the format yyyy-mm-dd.
i need the functionality like this:
if((date1>=start_date) && (date2<=end_date)){
}
please help me to solve this.
use strtotime to get the timestamps of your dates - this can be easily compared to each other like you already trying it.
If you have PHP 5.3, this is easily handled by the DateTime and DateDiff classes. If you have not, it is much harder, but look at the 'user contributed' section on DateDiff in the online PHP manual.