【问题标题】:How can I show next date and time from list of date time using PHP?如何使用 PHP 从日期时间列表中显示下一个日期和时间?
【发布时间】:2016-05-26 05:37:36
【问题描述】:

我在 mysql 数据库中有以下日期格式值:

1464436800 ( In php, date will be 2016-05-28 8:00 AM )
1464440400 ( In php, date will be 2016-05-28 9:00 AM )
1464444000 ( In php, date will be 2016-05-28 10:00 AM )
1464447600 ( In php, date will be 2016-05-28 11:00 AM )
1464552000 ( In php, date will be 2016-05-29 4:00 Am )

现在我想从这些数据库值列表中使用 php 显示下一个日期和时间。

例如:如果当前日期和时间是 2016-05-25 8:00 AM,那么我想像这样选择/显示下一个日期时间 2016-05-28上午 9:00

下一个日期和时间可能在 1 小时或 2 小时或 5 小时或 1 天或 2 天后等。

但是文本日期和时间显示在我的数据库中的日期时间值列表中,就像我展示的那样。

我怎样才能用 php 做到这一点?

更新:

<?php
$get_menu = mysqli_query($conn, "SELECT DISTINCT products.p_title, products.p_price, product_images.p_list_image, chef_profile.live_at, chef_profile.fname, chef_profile.lname, chef_profile.is_verified_chef, users.user_avator, chef_review.r_star, product_date_time.date, product_date_time.time FROM products LEFT JOIN product_images ON products.p_id = product_images.p_id LEFT JOIN chef_profile ON products.u_id = chef_profile.u_id LEFT JOIN users ON products.u_id = users.u_id LEFT JOIN chef_review ON products.p_id =  chef_review.p_id LEFT JOIN product_date_time ON product_date_time.p_id = products.p_id GROUP BY products.p_id");

$num_menu =  mysqli_num_rows($get_menu);
while( $get_result = mysqli_fetch_array($get_menu) ) {

$menu_title = htmlspecialchars(ucfirst($get_result['p_title']));
$menu_price = htmlspecialchars($get_result['p_price']);        
$menu_image = htmlspecialchars($get_result['p_list_image']) ? htmlspecialchars($get_result['p_list_image']) : "no-menu-preview.png";
$live_at =  htmlspecialchars($get_result['live_at']);
$fname = htmlspecialchars($get_result['fname']);
$lname = htmlspecialchars($get_result['lname']);
$date = htmlspecialchars($get_result['date']);        

if(!empty($date)) {
    $date_time = 'Next Date : ';
    $date_time .= date('D, F jS', $date);
    $date_time .= ' at ';
    $date_time .= date('G:00 A', $date);
} else {
    $date_time = 'No event time found';
}

$user_avator =  htmlspecialchars($get_result['user_avator']) ? htmlspecialchars($get_result['user_avator']) : "empty-photo.jpg";

$is_verified =  (int) $get_result['is_verified_chef'];
if($is_verified == 1) {
    $is_verified = '<small><i class="fa fa-check-circle "></i>&nbsp;Verified Chef</small>';
} elseif($is_verified == 0) {
    $is_verified = '<small>Not Verified</small>';
}
$r_star =  (int) $get_result['r_star'];

?>


<div class="col-md-4 col-sm-6">               
     <div class="items">
        <div class="img-effect">

            <a href="menu-details?<?php ?>"><img src="<?php echo IMG_DIR."menu_images/$menu_image" ?>" alt="" class="img-responsive"></a>
        </div>
        <div class="product-list">                
            <div class="col-md-9 no-p-m product-title">
                <h4><a href="product-list"><?php echo $menu_title; ?></a></h4>                        
            </div>
            <div class="col-md-3 price">
                <p class="text-right">$ <?php echo $menu_price; ?></p>        
            </div>
            <hr class="items-hr">
            <div class="row">
                <div class="col-xs-4">
                    <img src="<?php echo IMG_DIR."users_avator/$user_avator" ?>" alt="" class="img-responsive img-circle" width="100">
                </div>
                <div class="col-xs-8">
                    <p><strong>Chef <?php echo $fname . ' ' . $lname; ?></strong></p>                        
                    <em><?php echo $live_at; ?></em>                               
                    <div class="col-xs-12 col-sm-12 review">
                        <?php echo $is_verified; ?><br/>                                
                        <?php
                        if($r_star == 0 ) {
                              echo 'No Review';
                        } else {
                            for($x = 1; $x <= $r_star; $x++) {
                                 echo "<i class='fa fa-star'></i>";                                        
                            }
                            echo " ($r_star Review)";
                        }
                       ?>
                    </div>                     
                </div>                    
            </div>                
        </div>
        <div class="next-date">
            <p><?php echo $date_time; ?></p>
        </div>                
    </div>
</div>
<?php }  ?>

【问题讨论】:

  • 类似这样的$date = strtotime("+3 day", strtotime("2016-05-28 8:00 AM")); echo date("Y-m-d", $date)
  • 我更新了我的问题。请检查一下
  • 是的,我的脚本做你想做的事
  • 查看在线示例:https://3v4l.org/XsgIJ
  • 进行两个查询,一个只查询日期,一个查询所有内容。从第一个查询中创建数组并在第二个查询中显示它们。

标签: php


【解决方案1】:

您可以使用以下结构:

$newdate =  date('Y-m-d', strtotime("+30 days"));

你可以用你需要的每一天替换 30 个数字

【讨论】:

  • 不是每天。你检查我的日期时间值列表了吗?下一个日期可能在 1 小时或 3 小时或 7 小时或 1 天或 2 天 2 小时等之后。
  • 是的,我读到了。检查这个: $newdate = date('Y-m-d H:i:s', strtotime("+2 hour", strtotime("2016-05-28 8:00")));您可以 +1 小时或一天,然后 ....
  • 你也可以定义你需要的每个结构的输出格式:php.net/manual/en/function.date.php
  • 但不知道是+1还是+2还是day。
【解决方案2】:

在此 while 循环的运行时,您不知道所有事件时间是多少。因此你不能选择一个。他们正在流入(据我所知)。 您必须先拥有所有数据,然后才能选择下一个。

因此您有 3 个选项:

1。反转输出

通过反转输出(考虑到它按日期时间排序),您可以保留变量$nextEvent。我会说这是首选方法,如果您可以稍后在浏览器中对数据进行排序,或者按服务器排序无关紧要。

<?php
$get_menu = mysqli_query($conn, "[...] ORDER BY <date> DESC");

$num_menu =  mysqli_num_rows($get_menu);
$nextEvent = '';
while( $get_result = mysqli_fetch_array($get_menu) ) {

    [...]
    $date = htmlspecialchars($get_result['date']);        

    if(!empty($nextEvent)) {
        echo 'Next Event at: '.$nextEvent;
    }
    $nextEvent = $date;
    [...]
}

2。您将所有事件运行两次

全部运行两次意味着您有一个 while 循环并将数据保存在数组中,然后在输出期间运行该数组。 现在你有了所有的数据,我的方法仍然适用。

考虑到你有一个包含你的值的数组

<?php
$a = array(
    1464436800,
    1464440400,
    1464444000,
    1464447600,
    1464552000
);

您可以在array_values 上执行array_search 并获得您正在查找的元素的位置。如果你然后 +1 这个(考虑到你不是最后一个),你最终会得到下一个元素。

sort($a); // Just to be on the safe side, it is in the right order
var $index = array_search('1464444000',array_values($a));
if($index !== count($a)) {
    // next Element $a[$index+1];
} else {
    echo 'you are already at the last one'
}

3。您每次在循环中进行 SQL 查询

(不推荐)

您可以进行 SQL 查询以在每次处理事件时选择下一个事件。 但是这将是一个巨大的减速,因为必须运行大量的 SQL 查询。

【讨论】:

  • 您在我的问题中看到了我的 while 循环。我不知道下一个日期时间,所有日期和时间都将显示在 while 循环中,下一个日期和时间可以是任何时间。
  • 如果你可以反转输出你可以保存之前的时间(如果选择是按日期时间排序的)。但除此之外,恐怕你必须先写出数据然后循环遍历它。你无法预测下一个,如果它是“随机的”
  • 让我再解释一下:用户可以为他们的活动添加多个日期和时间。我使用strtotime 将所有选定的日期和时间插入数据库。所以我想显示我们当前时间的下一个事件日期时间
  • 编辑了答案以支持多种方法,每种方法的解释。希望一个适合你。
  • 我正在阅读它。但我可以看到我的查询显示来自数据库的第一个匹配日期和时间。如果有 4 个日期和时间,那么现在它向我显示第一个日期和时间。因此,如果我更改查询,例如:product_date_time.date > time(),。会有用吗?
【解决方案3】:

要先选择下一行,您需要使用 php 将时间转换为时间戳

echo $newdate = strtotime("2016-05-28 8:00");// get timestamp

然后使用查询从数据库中获取新时间

SELECT next_date FROM YOUR_TABLE WHERE next_date > $newdate ORDER BY next_date LIMIT 1;

你可以在 while 循环中创建 yo 数组

$array[] = htmlspecialchars($get_result['date']); 

要从数组中获取新值

$array = array(
    '0'=>1464436800,
    '1'=>1464440400,
    '2'=>1464444000,
    '3'=>1464447600,
    '4'=>1464552000
);

echo current(array_slice($array, array_search(0, array_keys($array)) + 1, 1));// pass your array key into array_search

查看DEMO

【讨论】:

  • OP 想要它在 PHP 中。 我怎样才能用 php 做到这一点.
  • @Saty 我在 php while 循环中显示所有日期和时间。在这个 while 循环中,我想显示当前日期和时间的下一个日期和时间。
  • 您可以创建一个date_time 的数组,并为每个日期显示增加该数组的索引。
  • 我不明白@Frayne Konok。
  • 查看@nv1t 发布的最后一个答案。
【解决方案4】:

试试这个..

将字段名称添加到您的数据库中以用作计数器。 例如:

+----------+----+
|dateTime  |cntr|
+----------+----+
|1464440400| 1  |
|1464444000| 2  |
|1464447600| 3  |
|1464552000| 4  |
+----------+----+

PHP/MYSQL:

$q1 = SELECT cntr FROM tableName WHERE dateTime = 'yourDate';

while($result = mysql_fetch_array($q1)){

    $cntr = $result['cntr']+1;//add 1 to get the next count in table

    $q2 = SELECT dateTime FROM tableName WHERE cntr = '$cntr';

        $data = mysql_fetch_array($q2);

        echo $data['dateTime']; //result
}

【讨论】:

    猜你喜欢
    • 2010-11-04
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 2015-12-23
    • 2014-01-18
    • 2023-03-22
    • 2019-06-23
    • 2016-07-29
    相关资源
    最近更新 更多