[英]a little more help in this simple script about date() function in PHP
why wont this work? 为什么不做这项工作? this is in a while loop: 这是一个while循环:
$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... 它在浏览器的“ if语句行”上给出了错误...
$postdate is something like 14 october before the if statement! $ postdate类似于10月14日的if语句!
Thanks 谢谢
分号必须位于大括号内,如下所示:
if ($postdate == date("j F")) {$postdate = "today";}
your $table .= bla bla bla "$postdate";
您的$table .= bla bla bla "$postdate";
line is wrong, it should read: 行是错误的,应显示为:
$table .= 'bla bla bla' . $postdate;
Your third line is formatted wrong. 您的第三行格式错误。 Try this: 尝试这个:
$table .= "bla bla bla $postdate";
You can let the MySQL server do the comparison (if you like) 您可以让MySQL服务器进行比较(如果愿意)
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. 如果日期等于“今天”,则today
的字段将包含1,否则为0。
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. 您可以使用Day() / Month()函数提取这些部分,尽管imo解决方案以这种方式失去了一些“优雅”。 And I would consider something like 我会考虑类似
SELECT ... (Date(insert_date)-Curdate()) mod 10000 as today ...
an ugly hack :( 丑陋的骇客:(
eg 例如
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.