gpt4 book ai didi

php - 在 PHP 中从多个日期范围创建日期数组

转载 作者:行者123 更新时间:2023-11-29 07:36:56 25 4
gpt4 key购买 nike

我有选择 SQL Server 的代码:

<?php
include "koneksi.php";
$sql = odbc_exec($koneksi, "select * from trip");
while (odbc_fetch_row($sql)) {
$no = odbc_result($sql, "number");
$start = odbc_result($sql, "start");
$finish = odbc_result($sql,"finish");
}
?>

这个循环包含以下数据:

|No|   Start  |  Finish  |
|1 |2018-01-01|2018-01-05|
|2 |2018-01-10|2018-01-13|

我想像这样制作数组:

array(
"2018-01-01",
"2018-01-02",
"2018-01-03",
"2018-01-04",
"2018-01-05",
"2018-01-10",
"2018-01-11",
"2018-01-12",
"2018-01-13"
);

如何从这个日期范围创建一个数组?

NB : looping can be more than 2 lines

最佳答案

对于结果的每一行,您可以使用 while() loop在数组中添加每个日期。要管理日期,您可以使用 strtotime :

while (odbc_fetch_row($sql))
{
// grab our results
$start = odbc_result($sql, 'start');
$finish = odbc_result($sql, 'finish');

// convert date to timestamp
$start_tm = strtotime($start);
$finish_tm = strtotime($finish);

// add dates until $finish_tm reached
while ($start_tm < $finish_tm)
{
// push new date
$dates[] = date('Y-m-d', $start_tm);
// move date marker to next day
$start_tm = strtotime('+1 day', $start_tm);
}
}

关于php - 在 PHP 中从多个日期范围创建日期数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com