Using dates

I need to do two things using dates, but I’m having some problems.

  1. I have a form with a date field, where the user can insert a date and this will be stored in a MySQL database. In the database dates are stored as “yyyy-mm-dd”, but I want the user to enter it as “dd-mm-yyyy”. How can I let the user write the date this way and transform it when the user submits the form?

  2. I need to calculate the difference between two dates. For example, I want to see every day a list of all the rows in the database that contain a date that is 7 days from now.

Thank you very much in advance for your help :slight_smile:

  1. I have a form with a date field, where the user can insert a date and this will be stored in a MySQL database. In the database dates are stored as “yyyy-mm-dd”, but I want the user to enter it as “dd-mm-yyyy”. How can I let the user write the date this way and transform it when the user submits the form?

Something like this would do the trick

$date = mysql_real_escape_string($_POST['date']);
$date = implode('-', array_reverse(explode('-', $date)));
  1. I need to calculate the difference between two dates. For example, I want to see every day a list of all the rows in the database that contain a date that is 7 days from now.

In a very simple form this should work

$res = mysql_query("SELECT date FROM table_name WHERE date > $mydate LIMIT 7");

This worked perfectly, thanks :slight_smile:

About your second piece of code, I am not sure, but what you have written returns maximum 7 results from the database, while I want all the database rows where the date is 7 days from now…

Anyway, I’ve solved it by transforming every date to epoch timestamps and then checking if their difference is 7 days :slight_smile: