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.
Related
I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!
I have a Mysql database where i keep dates of the clients orders.
Date is generated by a php function date('Y-m-d'). Two records in my database have values '0000-00-00'. One was created by a client from Australia and another by a client from Italy. Other 5000 orders are fine.
My question is how is it possible to generate such date ? Could it be something with individual browser settings ?
Thanks for help
The only reason what i can find is either your client has entered improper date format or he has entered an empty string.
Remember the date format in Mysql is yyyy-mm-dd.
I noticed I was having the same exact issue, and it turned out it was simply that I didn't put single quotes around my $date variable when inserting it, thus it was inserting a null or blank value which appeared as 0000-00-00 in the database. Once I put '$date' instead of $date in my insert statement, the value appeared as expected.
It means MySQL was passed an invalid value including no value at all. Check your code to make sure that it always has a valid value for that date.
Check this post default date '0000-00-00 00:00:00' or null. It will answer your question. Probably, year, month and day variables at their client side code have not been initialized.
EDIT
If that's not the case, then it seems you have recently changed the Date-related column in the database from VARCHAR or TEXT to DATE.
MySql changes all NULL values in this case to '0000-00-00'. So, if some time back, date has not been a compulsory field on the UI and if it was passed as NULL, then the above thing has happened.
It could be the case that you have purely programmed or outdated mysql function. For instance: CURDATE() + 5 will insert the date correctly in the DB, however if the user does it on 2016-12-28 the DB will show 0000-00-00. It will work fine if it the current date is 2016-12-20. The absolutely correct function would be CURDATE() + INTERVAL 5 DAY.
You write at the beginning of Jan, so maybe u have similar issue.
I have 3 dropdowns on a form for the user to choose their birthday. One for date, one for month and one for year.
Right now I cam preparing the date given by the user like this:
$date = sanitize($_POST['year']).'-'.sanitize($_POST['month']).'-'.sanitize($_POST['day']);
and inserting $date into the database in a DATE field. I want to be able to do operations based on this field's values, like sorting by date etc...
Is this the right way to prepare the data or should there not be any hyphens?
According to the MySQL manual page on DATE, the proper format is 'YYYY-MM-DD', so this appears as if it would work and allow you use all of the MySQL date and date comparison operations and functions.
However, you should consider validating user input before sending it to the database (never trust the security or validity of user input). Maybe you should run it through PHP's date() to make sure that the date you are inserting is valid:
$date = date('Y-m-d', strtotime($_POST['month'].'-'.$_POST['day'].'-'.$_POST['year']));
I agree, I would convert the text using strtotime, then validated it, although you have 3 dropdowns maybe a data like Feb 30 might throw things off.
I have a DATETIME string and I need only the DATE in my script to perform some searches in my database. Currently, I have two scenarios in my mind, but don't know which of them is faster.
The first scenario:
In my MYSQL database, I have two columns: datetime (which is a DATETIME type) and date (which is a DATE type).
Then, in my PHP script, each time I save a record, I will insert my known string to the datetime field, and then convert it to fit the date field (I was thinking of something like: $date = date("Y-m-d", strtotime($datetime))).
This way, all the necessary pieces are stored in my database and I can retrieve them on the fly (both the datetime and the date fields).
The second scenario:
The MYSQL database should consist only of the datetime column.
My PHP script will insert the known string to the datetime field without any other modifications.
And when I retrieve my data, I would do something like: SELECT datetime, DATE(datetime) FROM ...
Conclusion
Which of these scenarios is faster and therefore should be used? Should date formats be made on save or on retrieve? Is MYSQL faster than PHP on formatting dates? Is it better to store everything in the database and retrieve as it is, or store only the minimum and format on retrieve? Which of these scenarios is the best practice?
Thank you!
It depends of your usecases:
If you are only going to need the date for reading, then go with a single datetime column, conversion from datetime to date is cheap enough.
If you are going to select rows at a given date (like WHERE date = '2011-08-01'), then go for a date column, as this will allow mysql to use the indexes on the date column if you have added one.
If you are going to select rows in a date range, then go for a datetime column. You could do things like WHERE datetime >= '2011-08-01' AND datetime < '2011-08-16'.
The second one is the best and fast as you are getting the value based on the requirement. Rather getting some value and working on it later.
imho
datetime, or even unsigned integer (unix timestamp) is better for range filtering
datetime allow date-time function, it could be useful for aggregate function
avoid formatted data from mysql (that's mean raw)
anything related to presentation is PHP duty
Definitely depends on your situation - if you will be reading (a lot) more than writing, you can store both. But I'd go for storing one field (datetime) and convert that, either in PHP or while retrieving it from MySQL (convert datetime to char in the format you like)
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.