如何将事件的 date/time 转换为我可以使用的变量?
How do I convert the date/time of an event into a variable that I can use?
此代码有效,但有一个例外。我正在从日历中导入演出日期。当代码获取下一个演出日期时,我希望它读取它是在下午 6 点之前还是之后开始。如果之前,我希望它说 "Today" 如果它之后,我希望它说 "Tonight." 我已经尝试使用 $dateEvent("H"),但它说我正在调用一个功能。如何将事件日期转换为我可以使用的变量?
function calendar_dates() {
$servername = "localhost";
$username = "angelsdb";
$password = "R#start2013";
$dbname = "angelsice";
$fromToday = date("Y-m-d");
/// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT event_start_date, recurrence_interval, event_name, post_content, event_start_time, event_slug, event_all_day FROM wp_em_events";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$fromCalendar = $row["event_start_date"];
$dateEvent = new DateTime($fromCalendar);
$dateToday = new DateTime($fromToday);
if(strtotime($fromToday) <= strtotime($fromCalendar) && $row["recurrence_interval"] < 1){
if($dateToday == $dateEvent) {
/// THIS IS WHERE I WANT TO TEST IF IT IS BEFORE 6PM OR AFTER USING THE VARIABLE $GREETING
$time = new DateTime($row["event_start_time"]);
echo "<h2>" . $greeting . " @ " . $time->format('h:i a') . "</h2>";
echo "<a href='../events/". $row['event_slug'] . "'><h1>" . $row["event_name"] . "</h1></a>";
echo "<h3>" . $row["post_content"] . "</h3>";
} else {
$date = $dateEvent;
$time = new DateTime($row["event_start_time"]);
echo "<h2>" . $date->format('l') . " @ " . $time->format('h:i a') . "</h2>";
echo "<a href='../events/". $row['event_slug'] . "'><h1>" . $row["event_name"] . "</h1></a>";
echo "<h3>" . $row["post_content"] . "</h3>";
}
return;
}
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
试试这个
$dateEvent = "2011-07-26 20:05:00";//for example
$dateEvent = strtotime($dateEvent);
$time= date('H', $dateEvent);
if((int)$time>18)
echo 'Tonight';
else
echo 'Today';
将日期与下午 6 点的 DateTime
进行比较,查看 ideone
<?php
// your code goes here
$t1 = new DateTime("4:30pm");
$t2 = new DateTime("8:30pm");
function todayTonight($t) {
return (new DateTime("6:00pm") < $t) ? "Tonight" : "Today";
}
echo todayTonight($t1) . "\n";
echo todayTonight($t2) . "\n";
此代码有效,但有一个例外。我正在从日历中导入演出日期。当代码获取下一个演出日期时,我希望它读取它是在下午 6 点之前还是之后开始。如果之前,我希望它说 "Today" 如果它之后,我希望它说 "Tonight." 我已经尝试使用 $dateEvent("H"),但它说我正在调用一个功能。如何将事件日期转换为我可以使用的变量?
function calendar_dates() {
$servername = "localhost";
$username = "angelsdb";
$password = "R#start2013";
$dbname = "angelsice";
$fromToday = date("Y-m-d");
/// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT event_start_date, recurrence_interval, event_name, post_content, event_start_time, event_slug, event_all_day FROM wp_em_events";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$fromCalendar = $row["event_start_date"];
$dateEvent = new DateTime($fromCalendar);
$dateToday = new DateTime($fromToday);
if(strtotime($fromToday) <= strtotime($fromCalendar) && $row["recurrence_interval"] < 1){
if($dateToday == $dateEvent) {
/// THIS IS WHERE I WANT TO TEST IF IT IS BEFORE 6PM OR AFTER USING THE VARIABLE $GREETING
$time = new DateTime($row["event_start_time"]);
echo "<h2>" . $greeting . " @ " . $time->format('h:i a') . "</h2>";
echo "<a href='../events/". $row['event_slug'] . "'><h1>" . $row["event_name"] . "</h1></a>";
echo "<h3>" . $row["post_content"] . "</h3>";
} else {
$date = $dateEvent;
$time = new DateTime($row["event_start_time"]);
echo "<h2>" . $date->format('l') . " @ " . $time->format('h:i a') . "</h2>";
echo "<a href='../events/". $row['event_slug'] . "'><h1>" . $row["event_name"] . "</h1></a>";
echo "<h3>" . $row["post_content"] . "</h3>";
}
return;
}
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
试试这个
$dateEvent = "2011-07-26 20:05:00";//for example
$dateEvent = strtotime($dateEvent);
$time= date('H', $dateEvent);
if((int)$time>18)
echo 'Tonight';
else
echo 'Today';
将日期与下午 6 点的 DateTime
进行比较,查看 ideone
<?php
// your code goes here
$t1 = new DateTime("4:30pm");
$t2 = new DateTime("8:30pm");
function todayTonight($t) {
return (new DateTime("6:00pm") < $t) ? "Tonight" : "Today";
}
echo todayTonight($t1) . "\n";
echo todayTonight($t2) . "\n";