Working with a date in php sql
In general, the goal is as follows: You need to make a post placement for a time (1 year), i.e. in a year the post should not be displayed.
The idea was to create 2 variables in the database, 1 - the current date, 2-the end date of the post publication, and if the current date 1 becomes greater than the date 2, then we make the post not activated.
So the problem is that I don't understand how to work with dates in the table, what to use (datetime or timestamp) and what value to set in the table itself db. Reread a lot of information and nothing really helped. I create the end date of the post publication, but it is not recorded in the database.
$date = time();
$M = idate('m', $date);
$D = idate('d', $date);
$Y = idate('y', $date);
$H = idate('H', $date);
$i = idate('i', $date);
$s = idate('s', $date);
$date_end = date('Y-m-d H:i:s', mktime($H, $i, $s, $M, $D, $Y+1));
echo strtotime($date_end);
$sql = 'UPDATE `agrousadby` SET `mai_date_end` = '.$date_end.' WHERE `id` = '.$Param['id'];
mysqli_query($CONNECT, $sql);
2 answers
And why not just store the post creation time in the database as a single value DATETIME
(let's call it created_at
for certainty) and output only those records that are less than a year old, sifting out old records using the following WHERE-condition
SELECT
*
FROM
agrousadby
WHERE
created_at > NOW() - INTERVAL 1 YEAR;
'.$date_end.' The date in the request must be a string
'. "'" . $date_end. "'" . '