Datetime equal or greater than today in MySQL
0 34938
There are mainly three types that most commonly used to save date into the MYSQL table.
Date:
- It saves the date in YYYY-MM-DD format.
- It is the most popular type to declare a date in MYSQL.
- It only saves date, not time.
- It can save the values from '1000-01-01' to '9999-12-31'.
Datetime:
- It saves both date and time in YYYY-MM-DD hh:mm:ss format.
- Date can be possible from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
Timestamp:
- It also contains both parts means it can save date and time both.
- Date range possible for Timestamp is from '1970-01-01 00:00:01' to '2038-01-19 03:14:07'.
- For Datetime and Timestamp, we can use the following query to retrieve the date greater than or equal to current date and time in MYSQL.
Syntax:
SELECT * FROM timer WHERE date >= CURRENT_TIMESTAMP;
Here,
date is the name of the column and time is the name of MYSQL table.
Example:
<?php $con=mysqli_connect('localhost', 'root', '', 'astro'); // create connection with database echo "Current date and time is ".date('Y-m-d h:i:s')."<br>"; $query="SELECT date FROM timer WHERE date>=CURRENT_TIMESTAMP"; $result=mysqli_query($con,$query); if($result){ foreach($result as $row){ echo $row['date']."<br>"; } } ?>
Output:
We can use this example for Date type.
Example 2:
We can also retrieve a date greater than or equal to the current date with the help of Now() function.
<?php $con=mysqli_connect('localhost', 'root', '', 'astro'); // create connection with database echo "Current date and time is ".date('Y-m-d h:i:s')."<br>"; $query="SELECT date FROM timer WHERE date>=now()"; $result=mysqli_query($con,$query); if($result){ foreach($result as $row){ echo $row['date']."<br>"; } } ?>
Output:
Related Topic: PHP Date Time Set Function
Share:
Comments
Waiting for your comments