简体   繁体   中英

get date from database and check if this date has passed

I have a database that holds jobs. It holds the job name and the expiration date. My database is user_job(id, user_id, job_name, day, month, year) . The form that used in order to insert expiration date for the job in database included 3 drop-down lists. One that the user selected day(values 1-31) then month(values jan to dec) and year(2014 to 2024). I use the following function to get server's date:

<?php

$server_date = date('Y-m-d');


$a = mysql_query("select * from `user_job` where `user_id`='$session_user_id'  ");

while($run_job = mysql_fetch_array($a)){

  $the_job_day = $run_job['day'];
  $the_job_month = $run_job['month'];
  $the_job_year = $run_job['year'];
}   

?>

My question is if there is a possibility now to compare server's date with the job's expiration date in my database. And if expiration date has passed just to echo a message, "expired". Is there a posibility to do this?

$the_job_time = strtotime($the_job_year .'-'. $the_job_month .'-'. $the_job_day);
$current_time = time();
if ($current_time > $the_job_time) {
    // the job have expired
}

But I would suggest to store the time differently in the database. There are a bunch of different date/time types you can use

There is more than one way to do it, by order of effectiveness :

  1. Store your date as a date column in you database, then use a select statement to compare with NOW()
  2. Get the current timestamps and the timestamps of you job, then compare them.
  3. Use the DateTime class of PHP to use the DateTime::diff() method (see the documentation )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM