PHPLover
PHPLover

Reputation: 12957

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.

Upvotes: 0

Views: 1220

Answers (4)

Dave
Dave

Reputation: 3288

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

Upvotes: 1

Hayk Manasyan
Hayk Manasyan

Reputation: 506

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

Upvotes: 2

foxbeefly
foxbeefly

Reputation: 530

In MySQL:

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

Upvotes: 0

stackers
stackers

Reputation: 385

try this

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

Upvotes: 0

Related Questions