PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How to set a date for future in mysql

 October 11, 2022     mysql, phpmyadmin, sql, sql-timestamp     No comments   

Issue

I am creating a table that has 3 columns one primary key id the other one reg_date to hold the date that the user has registered and the last one exp_date which is 4 years ahead of reg_date to hold the expiry date of the account.

How can I set the exp_date to reg_date + 4 years?

Here's the code I've written so far:

CREATE TABLE Accounts (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  exp_date TIMESTAMP
)

Solution

Try this way,

CREATE TABLE Accounts (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  exp_date TIMESTAMP generated always as (reg_date + interval 4 year)
)


Answered By - Anup Patel
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing