Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何基于当前日期导入和打印CSV值?_Php_Csv - Fatal编程技术网

Php 如何基于当前日期导入和打印CSV值?

Php 如何基于当前日期导入和打印CSV值?,php,csv,Php,Csv,我有一个CSV文件,它是一个带有日期、小时和姓名的轮班时间表。知道如何根据当前日期/时间提取姓名吗?CSV如下所示: ... 14 Oct 2015, 02, 12:00 - 18:00, 6, "", "John Doe", "Joe Smith" 14 Oct 2015, 03, 18:00 - 00:00, 6, "Jenny Roe", "", "Henry Smith" 15 Oct 2015, 01, 00:00 - 06:00, 6, "Jake Blake", "Bob Ford

我有一个CSV文件,它是一个带有日期、小时和姓名的轮班时间表。知道如何根据当前日期/时间提取姓名吗?CSV如下所示:

...
14 Oct 2015, 02, 12:00 - 18:00, 6, "", "John Doe", "Joe Smith"
14 Oct 2015, 03, 18:00 - 00:00, 6, "Jenny Roe", "", "Henry Smith"
15 Oct 2015, 01, 00:00 - 06:00, 6, "Jake Blake", "Bob Ford", ""
...
我需要的是运行代码并打印计划下一班的人员的姓名,如:

Jenny Roe
Henry Smith
我知道我可以像这样以数组的形式加载文件:

<?php
$csv = array();
$lines = file('schedule.csv', FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}

echo '<pre>';
print_r($csv);
?>
$results[] = $data
...
01 Oct 2015 ,65 ,07:00 - 15:00 ,8 ,"","John Doe","Joe Smith","Martin Taylor","Henry Smith","Mike Miller"
01 Oct 2015 ,22 ,15:00 - 23:00 ,8 ,"","Bob Ford","Sarah Smith","Jack Williams","",""
01 Oct 2015 ,11 ,23:00 - 7:00 ,8 ,"","","Jenny Roe","Adam Davis","Jake Blake",""
02 Oct 2015 ,21 ,07:00 - 19:00 ,12 "Antonio Garcia","John Doe","Joe Smith","","Henry Smith","Mike Miller"
02 Oct 2015 ,22 ,19:00 - 07:00 ,12 ,"","Bob Ford","Sarah Smith","Jack Williams","",""
02 Oct 2015 ,11 ,07:00 - 15:00 ,8 ,"","","Jenny Roe","Adam Davis","Jake Blake",""
...

或将其打印为文本:

$currentDate = new DateTime(); //we create an object with the current time

$file = fopen("schedule.csv","r");

$results = array();

//we loop through the file, each line will be an array containing each fields
while (($data = fgetcsv($file, 0, ",")) !== FALSE)
{
    //we create an array that has three elements: the start time, the "-" and the endtime of the shifts
    $shiftTime = explode(" ", $data[2]); 
    //We create two datetime objects that mark the beginning of the shift and the end of the shift
    $startShift = dateTime::createFromFormat("d M Y H:i", $data[0] . " " . $shiftTime[0]);
    $endShift = dateTime::createFromFormat("d M Y H:i", $data[0] . " " . $shiftTime[2]);

    //We compare the two previously created objects to the current date and if 
    //the current date is between the start of the shift and the end of the shift, 
    //we know that person is scheduled to work.
    if($currentDate >= $startShift && $currentDate <= $endShift)
        $results[] = $data;
}


现在,我想知道从哪种方法开始会更好?如果有人能帮我解决这个问题,我保证我最终会学习PHP(doh!),并在这里发布回复作为证明。

我会尝试这样做:

<?php
$csv = array();
$lines = file('schedule.csv', FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}

echo '<pre>';
print_r($csv);
?>
$results[] = $data
...
01 Oct 2015 ,65 ,07:00 - 15:00 ,8 ,"","John Doe","Joe Smith","Martin Taylor","Henry Smith","Mike Miller"
01 Oct 2015 ,22 ,15:00 - 23:00 ,8 ,"","Bob Ford","Sarah Smith","Jack Williams","",""
01 Oct 2015 ,11 ,23:00 - 7:00 ,8 ,"","","Jenny Roe","Adam Davis","Jake Blake",""
02 Oct 2015 ,21 ,07:00 - 19:00 ,12 "Antonio Garcia","John Doe","Joe Smith","","Henry Smith","Mike Miller"
02 Oct 2015 ,22 ,19:00 - 07:00 ,12 ,"","Bob Ford","Sarah Smith","Jack Williams","",""
02 Oct 2015 ,11 ,07:00 - 15:00 ,8 ,"","","Jenny Roe","Adam Davis","Jake Blake",""
...
它实际上将$data的内容附加到$results数组的末尾。最后,你会有这样的结果:

<?php
$csv = array();
$lines = file('schedule.csv', FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}

echo '<pre>';
print_r($csv);
?>
$results[] = $data
...
01 Oct 2015 ,65 ,07:00 - 15:00 ,8 ,"","John Doe","Joe Smith","Martin Taylor","Henry Smith","Mike Miller"
01 Oct 2015 ,22 ,15:00 - 23:00 ,8 ,"","Bob Ford","Sarah Smith","Jack Williams","",""
01 Oct 2015 ,11 ,23:00 - 7:00 ,8 ,"","","Jenny Roe","Adam Davis","Jake Blake",""
02 Oct 2015 ,21 ,07:00 - 19:00 ,12 "Antonio Garcia","John Doe","Joe Smith","","Henry Smith","Mike Miller"
02 Oct 2015 ,22 ,19:00 - 07:00 ,12 ,"","Bob Ford","Sarah Smith","Jack Williams","",""
02 Oct 2015 ,11 ,07:00 - 15:00 ,8 ,"","","Jenny Roe","Adam Davis","Jake Blake",""
...

好了。我希望这能把事情弄清楚。

谢谢你对Osuwariboy的所有帮助!这感觉像是一场艰苦的斗争,但最终还是奏效了。多亏了你

我将尝试用解决方案全面描述该案例:

我的schedule.csv文件格式如下(日期、未使用的编号、轮班时间、轮班长度、名称(最多6个):

这里的主要问题是,我要找的不是当前计划的人,而是下一班的人。班次是8小时(常规)或12小时(周末和一些随机的日子)。基于此,我不能简单地看8小时或12小时,因为这会产生各种问题,如:

  • 在12小时班次开始时向前看8小时(显示相同的班次)

  • 在8小时轮班结束时展望前方12小时(跳过下一班)

轮班日期也存在问题,例如2015年10月1日23:00-7:00开始时为10月1日,结束时为10月2日

根据Osuwariboy的提示,我提出了以下解决方案:



也许它不是最漂亮、最干净的代码,但是,嘿,它似乎在任何情况下都能工作。我希望有人也会觉得它有用。

谢谢!这看起来很好。我可以理解你的操作方式,但我不太清楚的是开始时的
$results=array();
(它代表什么?)在最后的
if
-(
$result[]=$data;
)至于如何打印本例中的输出?我在文章中添加了一些解释。如果事情仍然不清楚,请不要犹豫再次询问。正如我预期的那样,但仍然让我感到困惑。谢谢!问题是我在填充
$results
时遇到问题。
schedule.csv
包含有效条目,
$startShift
$endShift
填充正常,但在
if
语句中,无法读取任何内容
$results
-只有一个空的
数组()
。当
$currentData
是Unix时间戳,而其他值类似于DateTime对象时,您确定我可以比较时间吗?我添加了
date\u default\u timezone\u set('Europe/London');
以确保生成的时间正确,并且我尝试了
$currentDate=date('Y-m-d H:I:s:u'))
-不行。你说得对,我可能弄错了$currentDate。你试过了吗?$currentDate=new DateTime();,这应该会创建一个值为“now()”的DateTime对象(与系统的当前日期和时间相同)。这将允许你在我的最终“if”中使用比较结果。