PHP tarihiyle ilgili bu basit bir script biraz daha yardım () fonksiyonu

4 Cevap php

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";

benim tarayıcıda 'if deyimi satır' ile ilgili bir hata veriyor ...

$ PostDate eğer deyimi önce 14 Ekim gibi bir şeydir!

Teşekkürler

4 Cevap

Noktalı virgül, bu gibi, parantez içinde olması gerekir:

if ($postdate == date("j F")) {$postdate = "today";}

Lütfen $table .= bla bla bla "$postdate"; çizgisi yanlıştır, bu okumalısınız:

$table .= 'bla bla bla' . $postdate;

Üçüncü hat yanlış biçimlendirilir. Bu deneyin:

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

Sen MySQL sunucusu karşılaştırma yapalım (isterseniz)

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 ...

çirkin bir hack: (

örneğin

$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";
}

baskılar (şu anda benim makinede)

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