我希望你们有时间让一只老狗(71 岁)学习新技巧。我有大约一周的 PHP 和 mysql 经验,但我在学习它的工作原理时获得了一些乐趣,并且取得了一些进步。但是我有这个查询/逻辑/语法问题,如果您不要对我的曲折大笑,我想得到一些意见。
我有一个日历程序无法按照我想要的方式发出提醒。我想我会为了好玩而尝试弄清楚它,但我不能更改表格的内容,以免破坏整个程序。我成功地让它做了我想做的事,即通过获取“时间”表条目并将它们排序并过滤成我想要的内容,在一封电子邮件中发送我一天的所有提醒。但几天后,我注意到在我的时间 17:00(格林威治标准时间 -7)之前的任何条目都很好,但在那之后的任何条目都是垃圾。因此,仔细查看表格,我注意到格林威治标准时间 23:59 之后的任何时间都是 5 位数,而之前的任何时间都是 6 位数。所以我发现了问题。我认为我需要做的就是在中间放置一个 IF 并相应地添加额外的数字,使用 strtotime 将其更改为 UNIX 纪元时间,然后执行 date() 并仅使用小时和分钟。你还在笑吗?让我重申一下,我对 PHP 和 mysql 知之甚少,但我正在学习,这让我很高兴,也许还年轻了一点。
这是我得到的。我只会发送查询,因为连接到数据库并发送邮件工作正常。第一个有效...除了前面提到的问题...
date_default_timezone_set('America/Denver');
$today = date("Ymd");
mysql_select_db($dbname);
$query = sprintf("SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = '".$today."'");
$result = mysql_query($query);
$body = '';
$mail = '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$mail = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[0], date("H:i",((strtotime($row[1])-25200))), $row[2]);
}
mysql_free_result($result);
这是真正缺少某些东西的疯狂修复......
date_default_timezone_set('America/Denver');
$today = date("Ymd");
mysql_select_db($dbname);
$query = sprintf("SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = '".$today."'");
$result = mysql_query($query);
$body = '';
$mail = '';
$stt = '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$mail = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[0], ($stt = { if ($row[1] < 100000, + 1234500000) ELSE ($row[1] + 1234000000) }) date("H:i",((strtotime($stt)-25200))), $row[2])
};
我希望你们中的一个人愿意接受这个并幽默一个老家伙......不要笑了!
最佳答案
尝试以下操作。我添加了评论以便于理解:
/* I assume that the times and dates in your database are stored in UTC,
so set the UTC timezone first. */
date_default_timezone_set('UTC');
/* $today might now contain a value of tomorrow if it's past 17:00 in your timezone.
This is required though, as there could be an event at e.g. 18:00 of your time which would
not be returned otherwise. sprintf is not needed, as you don't format anyting here */
$today = date("Ymd");
$query = "SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = " . $today;
mysql_select_db($dbname);
$result = mysql_query($query);
$body = '';
$mail = '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// Store the value of the returned time in a $fixedTime
$fixedTime = $row[1];
/* Simple approach: While the length of the time is less than 6 digits, prepend a leading 0
This will even work for times like 00:00:15 */
while (strlen($fixedTime) < 6)
$fixedTime = '0' . $fixedTime;
// Now store the unix timestamp (still in UTC!)
$unixEpoch = strtotime($fixedTime);
// Set the timezone to your local time ...
date_default_timezone_set('America/Denver');
// ... and $formattedTime receives the formatted time value in your timezone
$formattedTime = date("H:i", $unixEpoch);
// Now create the mail content
$mail .= sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[0], $formattedTime, $row[2]);
}
mysql_free_result($result);
不过,包含时间和日期值的数据库列应声明为 DATE
、TIME
或 DATETIME
。
这将使这里的事情变得容易得多,因为 MySQL 可以处理时区转换和日期/时间格式化。
关于php - 由于位数不同,sql 表中的时间查询不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9528577/