Store month and year in mysql database - php

I have following date:
$params['ExpirationMonth'];
$params['ExpirationYear'];
I need some suggestion regarding how can I store only month and year in mysql database. After some research I have found 3 ways to do it.
1) I can use 1st as the days and can store along with month and year.
2) Instead of using date I can use varchar datatype and can only store month and year in it.
3) I can store the two as two separate columns as integers. i.e. for month and year.
Which is the better way?. Need some suggestion. Thanks.

You can use a date column, keeping in mind that:
You could set the day portion to 1
The card is good for the entire month of the specified expiry date. 0512 means valid through 31 May 2012.
Advantages:
Sorting is straight forward (compared to storing as varchar)
Date operations are straight forward (compared to storing as varchar/two integer columns)
Data stays in one column (compared to storing as two integer columns)
Sample queries:
-- expires in 2012
WHERE expires >= '2012-01-01' AND expires < '2012-01-01' + INTERVAL 1 YEAR
-- expires in Dec 2012
WHERE expires >= '2012-12-01' AND expires < '2012-12-01' + INTERVAL 1 MONTH

1) I can use 1st as the days and can store along with month and year.
This option is better as if there is any calculation or sorting need
to be done, it will be useful and handy.
2) Instead of using date I can use varchar datatype and can only store month and year in it.
3) I can store the two as two separate columns as integers. i.e. for month and year.
For above two option are fine as it will take less space as compare to
date or timestamp. But you can do calucation or orderby with this. For
calculation you need to convert it date by "str_to_date" function
I hope this will help you in some way.

stored as UNIX. suddenly need to add something in the future, and no problem to get anything out of date

Expiration Month/Year from credit cards means that this card is valid until the end of this Month/Year.
Best way to store as a comparable date you should get the last day of this month
STR_TO_DATE( CONCAT( '01,', #Month, ',', #Year, '%d,%m,%Y )
+ INTERVAL 1 MONTH
- INTERVAL 1 DAY

You can always store it like YYYYDD as an integer:
So for May 2021 you can store this integer: 202105

Related

show records from sql created during specific week

I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.

Select date stored as Unix Timestamp and compare with given Unix TimeStamp

I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);

Replace DAY at end of Date

I have a column in MySQL stored as: 2014-03-16 (for March 16, 2014). It has been decided that the fields should all be changed to the first day of the month -- whatever months in which they occur. I have records that span from 1998 to present day, several thousand of them. Is there an easier way to change just the DAY (in this case, 16) on all the records besides REPLACE 2014-03-16 with 2014-03-01?
I am using PHP, so if it's easier to change it there let me know.
You can do it using a MySQL query:
UPDATE your_table
SET date_column = DATE_FORMAT(date_column ,'%Y-%m-01')

MySQL WHERE date smaller than Y-m

Evrey row in my table has a date Y-m-d. Now I want to select only those rows that have a date from before a certain month and year independet of the day. So for example I want all rows with a date befor january 2014.
How would I do this in MySQL? Only considering the month does not work, because it will give me also rows with a month smaller, but a year after my year. E.g. i want rows with date smaller than 03/2013. It will give me also 02/2014.
Of course I could in php define first the first day of the month and than compare the full date. But this does no seem too great and when I want to compare dates after a certain month I have to know how many days a month has.
You can use the STR_TO_DATE function to convert to actual DATE type and then do a simple comparison to the cut-off date.
SELECT *
FROM Your_Table
WHERE STR_TO_DATE(Your_Date_Column,'%Y-%m-%d') < STR_TO_DATE('2013-03-01','%Y-%m-%d');

mysql query, how to go about it?

I am storing data in a mysql database with unix time()
I want to select data out for days, and echo it out like this:
Monday - value
Tuesday - value
Wednesday - value
Thursday - value
Friday - value
Saturday - value
Sunday - value
value is saved in the database as value
the times are saved in the database as time
So basically I want to select time from the database in a query that groups all of them as a day, Monday.. do you get what I mean?
Any help with this would be greatly appreciated.
First, replace that integer with a real DATETIME column, then use MySQL's built-in functions to easily GROUP BY the DAYOFWEEK(column)
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
There is no advantage to storing an integer instead of a real date. Not only can you easily convert to a UNIX timestamp if you need that in your application, but you'd save storage space too.
To convert to MYSQL native date, use FROM_UNIXTIME(). DAYNAME will return the day of the week.
SELECT DAYNAME(FROM_UNIXTIME(col))
For example:
SELECT DAYNAME(FROM_UNIXTIME(1196440219))
Further, to group by you could use something like this:
SELECT DAYNAME(FROM_UNIXTIME(col)), ... FROM table GROUP BY DAYNAME(FROM_UNIXTIME(col))
Maybe hits post can help you: MySQL Query GROUP BY day / month / year
Regards!

Categories