简体   繁体   中英

How to check the date in 'YYYY-MM-DD' format with the date present in 'Unix Time stamp' format in PHP?

The table event has a column start_time , which holds the date in Unix Time stamp format.

Now I want to fetch all the records from the table whose date falls on the date given by user in YYYY-MM-DD format (like 2015-09-17 ).

How should I do this using PHP? I can't use ready made MySQL functions for this.

You can user

MySQL DATE_FORMAT() Function or PHP DATE() Function

for formatting date. MySql example:

SELECT DATE_FORMAT(event.start_date, "%Y-%m-%d") FROM DB_NAME

PHP example:

$start_date = 1442838220; // Unix Timestamp
$formated = date('Y-m-d', $start_date); // YYYY-MM-DD format

What have you tried? What are your column names etc?

Having said that something like this

$date = new DateTime($userinputdate); // format: YYYY-MM-DD
$epoch_date = $date->format('U'); 

$sql = "SELECT * FROM `event` WHERE `start_time`='$epoch_date'";

Sort your sql injection out use pdo/prepares and you should be fine. I've not done the actual execution as you don't mention if you're using mysqli or PDO but that you should be able to work out yourself pretty easily

try this

$timestamp=1442838154;
echo date("Y-m-d", $timestamp);

在MySQL中:

SELECT * FROM `event` WHERE DATE(`start_time`) = CURDATE();

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