PHP comparing strings -


this question has answer here:

i have dates , timestamps stored every item in table, format of month/day/year respectively hour/minute. topology looks following:

--------------------------------- | name  | id | date     | time  | --------------------------------- | item1 | 1  | 04.15.13 | 09.17 | --------------------------------- | item2 | 2  | 04.17.13 | 12.58 | --------------------------------- | item3 | 3  | 04.18.13 | 22.43 | --------------------------------- 

and on. using php date() function retrieve current date , time of server this:

$date = date(m) . "." . date(d) . "." . date(y); $time = date(h) . "." . date(i); 

as writing this, give me $date = "04.18.13" , $time = "19.05".

if current date , time past date , time stored specific item in database, want item flagged outdated/inactive when user browsing it. instance:

  • if it's april 16th 11.05, item1 inactive item2 , item3 still active.
  • if it's april 17th 13.02, item1 , item2 inactive item3 still active.
  • if it's april 18th 20.38, item1 , item2 inactive item3 still active.

i lost if simple operators >, <, >=, <= work since i'm working strings. how compare dates , times retrieved database current date , time?

summary: how compare 2 strings, holding either date or time, each other figure out if item active or not?

you should create datetime objects strings. this:

# assume you've fetch date , time mysql $time_from_db = datetime::createfromformat('m.d.y h.i',              $mysql_record['date'] . ' ' . $mysql_record['time']);  $now = new datetime();  # of php 5.2.2, datetime objects can compared using <> == operators if($now >= $time_from_db) {     echo "it's in past."; } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -