This returns 1
(aka TRUE
)
SELECT DATE_SUB(NOW(), INTERVAL 24*100 HOUR) = DATE_SUB(NOW(), INTERVAL 100 DAY);
100 days ago, the hour of day does not change. But due to Daylight Savings Time (US), 100 twenty-four hour periods ago is actually one hour earlier than if you counted by days. If the above statement accounted for DST, it would return 0
or FALSE
.
Is there a way I can say to account for DST for a given statement or session? I would prefer not to use UNIX_TIMESTAMP
since it cuts off anything past 2038.
The DATE_ADD() function adds a time/date interval to a date and then returns the date.
You can use str_to_date to convert a date string to MySQL's internal date format for inserting.
MySQL WEEKDAY() Function The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.
You'll need to create a custom function, something like this.
DELIMITER $$
CREATE FUNCTION DST(ADatetime DATETIME) RETURNS DATETIME
BEGIN
DECLARE result DATETIME;
SET Result = ADatetime;
IF ADatetime >= startDST AND ADateTime <= endDST THEN
result = DATE_SUB(ADatetime, INTERVAL 1 HOUR);
END IF;
RETURN result;
END $$
DELIMITER ;
This is really just a matter of converting to UTC and back:
CONVERT_TZ(DATE_SUB(CONVERT_TZ(NOW(),@@session.time_zone,'UTC'), INTERVAL 24*100 HOUR),'UTC',@@session.time_zone);
This assumes you have the timezone tables set up to use named time zones. If not, you can use '+0:00' instead of 'UTC'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With