Reputation: 1816
I am creating a application where I am storing the details of users. One of the field is the time created and number of days the user want his account to be active.
I am using date('c')
to store the time in created
table field. Now if the user selects that he wants his account to be active for only one day, how can I add that one day to the current date('c')
and store it into my next table field valid_till
.
Later I was planning to check if the current date('c')
is between the created
and valid_tll
field, then the users account is active, else the account is deleted.
I want to use sql triggers to delete the account.
Upvotes: 0
Views: 78
Reputation: 7470
Here's a SQL based solution. You can translate this into an update or insert.
SELECT
`created`,
CONCAT(
DATE_FORMAT(
DATE_ADD(SUBSTRING(`created`,1,10), INTERVAL 1 DAY), '%Y-%m-%d'
),
RIGHT(`created`,15)
) AS 'valid_till'
FROM `some_table`
But I would recommend using the SQL UTC_TIMESTAMP()
instead of the PHP date function and a row with the TIMESTAMP
datatype instead of a (VAR)CHAR
for you to make easier calculations in the SQL side.
Upvotes: 1
Reputation: 689
Why not use SQL, depending on the engine you are using, you can use something like create= NOW(), expires NOW() + interval number_of_days, sql engines handles the dates for you and you wont need to go to the troubles of creating php times and work with them..
Upvotes: 1
Reputation: 9480
You can use PHP's strtotime():
$valid_till = date('c', strtotime('+1 day'));
You can read more about the possible formats on in the Supported Date and Time Formats section of PHP manual.
Upvotes: 2