TFK300
TFK300

Reputation: 41

Checking if the date/time is less than a date provided?

I'm implementing the following in PHP with stores this string in the DB for the "created_at" column: Wed, 17 Dec 2014 14:53:02 +1100.

$created_at = date('r', $insta['created_time']);

Now I'd only like to do the insert if $created_at is more than a certain date, for example:

if ($created_at > "Wed, 15 Dec 2014 00:00:00 +1100") { //insert in to db }

That doesn't work though. I'd usually use strtotime() but unsure how to go about it when the data is set in that format. Also, the column type is varchar(255).

Upvotes: 0

Views: 1298

Answers (4)

EternalHour
EternalHour

Reputation: 8621

Aside from your question, you need to store datetime data in a datetime column. Mysql has specific functions and formatting for this reason. But, in order to store it will need to be in Y-m-d H:i:s format. Doing this will save you big hassles down the road.

In regards to your question, you can use PHP's DateTime class if you are using PHP version 5.2+.

$compareTime = new DateTime('2014-12-15 00:00:00'); //new datetime object//
$createdTime = new DateTime($created_at); //converts db into datetime object//

if ($createdTime > $comparedTime) { ..insert into DB.. }; //compare them//

Upvotes: 3

vijay ojha
vijay ojha

Reputation: 21

Use strtotime().

$created_at = strtotime($insta['created_time']);
$checkdate = strtotime(date('Y-m-d H:i:s')); //here we have to insert check date
if($created_at > $checkdate){ 

}

Upvotes: 0

Mubashar
Mubashar

Reputation: 12668

for PHP version >= 5.3

Use date_create_from_format or DateTime::createFromFormat

Although I think strtotime would also work but to be sure pass date time format and value to above function. For detail of format see documentation.

Upvotes: 0

Raj K
Raj K

Reputation: 371

Why are you storing WED in database..just store date with the php function date("Y-m-d H:i:s") and the table field type will be date..

This way mysql automatically give u facility to compare two dates. You can simply use if else statement to compare two dates.

Upvotes: 0

Related Questions