PHP [SOLVED]: Not understanding mysql statement DATE_ADD(NOW() INTERVAL)

PHP [SOLVED]: Not understanding mysql statement DATE_ADD(NOW() INTERVAL)

Home Forums Scripting PHP Tutorials PHP [SOLVED]: Not understanding mysql statement DATE_ADD(NOW() INTERVAL)

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245524

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am working on a page which contains the following line of mysql. The overal objective is simply to match the current week num to the current date

    $sql = "select distinct weekNum from " . DB_PREFIX . "schedule where DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR)"
    

    As per my research DATE_ADD() allows you to add a certain number of days to a date.

    Thus I am assuming DATE_ADD(NOW() adds the current date to…well date?

    What exactly does INTERVAL do in this statment.

    Any help interms of an explanation clarifying above statment will be much appreciated.

    #245525

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    As far as I can see this statement is trying to manage timezone difference

     INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR
    

    should return difference(in hours) for different timezone(server and offset) which is then added to current date to calculate actual time for scheduling.

    from https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

    DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
    These functions perform date arithmetic. The date argument specifies the starting date or datetime value. expr is an expression specifying the interval value to be added or subtracted from the starting date. expr is a string; it may start with a – for negative intervals. unit is a keyword indicating the units in which the expression should be interpreted.

    Source: https://stackoverflow.com/questions/48019038/not-understanding-mysql-statement-date-addnow-interval
    Author: BlindSniper
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.