Cannot save today's date in mySQL column - php

This is a simple solution, but I cannot get it to work even after reading many similar posts on the topic.
I am able to define a php variable to hold the current date because I can echo it properly:
$_date_today = date('Y-d-m');
echo $_date_today;
2017-31-03
But when I try to use that php variable to insert the date value into mySQL table, the value inserted is 0000-00-00. My date_order column in the table is defined as DATE.

While mySQL always stores the date in the database using the same format and I don't believe it can be changed, you can format the date however you want when retrieving it using the many mySQL functions available... see the following link: mySQL date manimpulation functions

try to change the date format cause mysql format is Y-m-d and you are entering Y-d-m
try use this $_date_today = date('Y-m-d'); insted of $_date_today = date('Y-d-m');

Related

Convert date stored as VARCHAR in MYSQL to MYSQL Compatible Date [duplicate]

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.

error in save date in database with php

want to be able to store the current date from PHP in to a mysql table.
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";
Date's column type in database is DATE.
but date save in database like this: 0000-00-00
help me.
try this
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."',".$date.",'".$yes."')";
or
if you want to inset today's date use
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
I need to store Date field in database for example VARCHAR(10) becouse, DATE type in mysql server don't support persian (farsi) date.
If you are getting the current date, just use one of the built-in MySQL functions for that:
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.
Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.
You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.

How to hide records when the date has passed?

I am unsure on how to check whether a date has passed in order to only show rows where the date has not passed.
I am using CodeIgniter and when the date is set, I'm not using a date type (using varchar) in the MySQL table as I'm using the jQuery date-picker to set the date.
So when the date is set, I need to pull the date from the table to check whether the date has passed or not?
So I'm not sure whether I have to totally change my date input to use the date type in the MySql table in order to pull the date and check whether it is < now()
Any guidance or advice would be extremely appreciated :)
Is there any particular reason for NOT using the date type in MySQL? If is a date, why not store it as a date? It will still be usable as a string in your PHP script when you retrieve it from the database.
You should always use the date type in MySQL if you are tracking dates, that way you have all the date functions available to you. The date functions of MySQL are much faster than trying to parse them as text with PHP.

how do i get day only from javascript calender and store it into database

I had used javascript calender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved.
How can I do that as I cannot make changes in javascript code as i m not good at it.
$date_customer=date("d",strtotime($_POST['datum1']));
I had also tried it by changing the column name to "tinyint" but didn't work :( .... it only stores 127 and shows 1 when record is viewed from database.
Instead of sending date to server you could send the day by using
.getDay() method of javascript Date object.
I dont know the format of your date you get in your text input (when you click on one of the days in your calendar) but i'd suspect it to be dd/mm/yyyy or mm/dd/yyyy
So your php will need to be the following to only get the day
$date = explode("/",$_POST['datum1']);
// if format is dd/mm/yyyy then use the below
$date_customer = $date[0];
// otherwise if format is mm/dd/yyyy then use the below
$date_customer = $date[1];
Check out the explode function
i would save the date in MYSQL as an INT by using this function (save it as a unix timestamp) which would be helpful in comparing dates later on (up to the second) or add/remove days/years/months .
the idea would be send the whole date string generated by javascript to the PHP script "dd/mm/yyyy" ,
then in php using the explode function and create the unix timestamp using the mktime function
then save it to the database as an int ,
then when you want to read it , use the php date function to know the day/month/year/hour/second/minute , you could then also add hours (+3600) or days (+3600*days) etc... , or even get range of dates and many other functionalities you may use later ...
cheers
I suppose that you use mysql (TINYINT are mysql specific).
TINYINT are integer and in php integer are usually not prefixed by zeros
Use a DATE field and format it when you report it.
You can use mysql date_format(date,"%d") function in your query.
Or the php date function.
select date_format(date_field,"%d") from some_table;
You can use a VARCHAR(2) to store the date (ugly).
If you stick to store only the DAY in an int or tynint.
use sprintf("%02d",$day) to format it correctly in php.

php date() mysql datetime

I want to use PHP to insert date into mysql datetime field.
$time = date('Y-m-d');
mysql_query(INSERT INTO A (date) VALUE ('$time'));
Table A date column is datetime field. I want it shows 2011-01-24. But it always shows 2011-01-24 00:00:00. So my problem is how to remove the suffix "00:00:00".
Since the field is a DATETIME field, it stores the DATE and the TIME. If you want to exclusively store the date, make it a DATE field.
Change the column type to DATE instead of datetime!
A bit unclear, I see 2 possible solutions:
If you're referring to removing the suffix 00:00:00 from the MySQL, you need to change the field type from datetime into a date.
If you're referring to the removal of the suffix from the php script, well you can always use
mysql_query(SELECT DATE_FORMAT(date, '%d-%m-%Y') FROM a);
read up mysql date and time functions
Change the field to a DATE field in your table. Your PHP code is fine as it is.

Categories