tags:

views:

35

answers:

4

Hi.

why wont this work? this is in a while loop:

$postdate = date( "j F", strtotime( $row['insert_date'] ) );
if ($postdate == date("j F")) {$postdate = "today"};
 $table .= bla bla bla "$postdate";

it gives an error on the 'if statement line' in my browser...

$postdate is something like 14 october before the if statement!

Thanks

+3  A: 

your $table .= bla bla bla "$postdate"; line is wrong, it should read:

$table .= 'bla bla bla' . $postdate;
knittl
whatever, the line above is the faulty code right now, i just wrote that quickly
Camran
ah yes... that too!
clops
+2  A: 

Your third line is formatted wrong. Try this:

$table .= "bla bla bla $postdate";

ChrisR
I think $table .= "bla bla bla {$postdate}"; would be slightly more correct
Joe Philllips
yes ... really very absolutely slightly =)
ChrisR
+4  A: 

The semicolon needs to be inside the braces, like this:

if ($postdate == date("j F")) {$postdate = "today";}
clops
thanks! missed that
Camran
A: 

You can let the MySQL server do the comparison (if you like)

SELECT
  x,y,z,insert_date, Date(insert_date)=Curdate() as today
FROM
  foo
WHERE
  somecondition....

The field today will contain 1 if the date equals "today" or 0 otherwise.
edit: you're "only" comparing the day/month part of the timestamp. You can extract these parts with the Day()/Month() functions, though imo the solution loses some of its "elegance" that way. And I would consider something like

SELECT ... (Date(insert_date)-Curdate()) mod 10000 as today ...

an ugly hack :(

e.g.

$pdo = new PDO('mysql:host=...;dbname=...', '...', '...'); 
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

// example table 
$pdo->exec('CREATE TEMPORARY TABLE foo (id int auto_increment, insert_date TIMESTAMP, primary key(id))');
// insert three rows, only the second is "for today"
$pdo->exec('INSERT INTO foo (insert_date) VALUES (Now()-Interval - 2 day), (Now()), (Now()-Interval + 1 day)');

foreach( $pdo->query('SELECT id, insert_date, Date(insert_date)=Curdate() as today FROM foo') as $row ) {
  echo
    $row['today'] ? '+' : '-',
    $row['id'], ' ', $row['insert_date'], "\n";
}

prints (right now on my machine)

-1 2009-10-21 17:03:55
+2 2009-10-19 17:03:55
-3 2009-10-18 17:03:55
VolkerK