【问题标题】:PHP datetime / datetimezone relative to user input with valid input checkPHP datetime / datetimezone 相对于用户输入的有效输入检查
【发布时间】:2014-06-01 06:15:26
【问题描述】:

请原谅我,我刚开始学习 PHP,但是我很好地坚持了我的导师让我弄清楚的这个示例项目。

目标是首先显示 4 个预设时区的本地时间,然后允许用户手动输入时间,并在提交时显示相对于手动输入时间的 4 次中的每一次的时间。

检查用户输入是否有效也很重要,而不仅仅是传递任何输入而不管它可能是什么。

我的 IF 语句不正确并且没有输入匹配,但我无法看到问题..

如果有人能指出我正确的方向,我将不胜感激。我已经广泛阅读了 PHP.net 上的文档,并且已经阅读了 dateTime 上的所有内容,但也许我还没有把它们放在一起。

<?php
    $timezones = array(
      'EST' => -5*3600, //equador
      'UTC' => 0, //london
      'CCT' => +8*3600, //beijing
      'PST' => -8*3600, //los angeles
    );

    foreach ($timezones as $time) {
        $now = time();
        $now += $time;
        print gmstrftime('DATE: %B %d %Y and TIME: %r <br/>',$now);
    }

        $nowM = date('m');
        $nowD = date('d');
        $nowY = date('Y');
        $nowHr = date('h');
        $nowMin = date('i');
        $nowSec = date('s');
?>

<form action="homework2.php" method="post">
    <label>Sync Timecode</label>
    <input type="text" name="month" placeholder="Month <?php $nowM ?>" value="<?php $nowM ?>" />
    <input type="text" name="day" placeholder="Day <?php $nowD ?>" value="<?php $nowD ?>" />
    <input type="text" name="year" placeholder="Year <?php $nowY ?>" value="<?php $nowY ?>" />
    <input type="text" name="hour" placeholder="Hours <?php $nowHr ?>" value="<?php $nowHr ?>" />
    <input type="text" name="min" placeholder="Minutes <?php $nowMin ?>" value="<?php $nowMin ?>"/>
    <input type="text" name="sec" placeholder="Seconds<?php $nowSec ?>" value="<?php $nowSec ?>"/>
    <input type="submit" id="button">
</form>

<?php
    $format = 'd-m-Y h:i:s';
    $queryTime = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'] . " " . $_POST['hour'] . ":" . $_POST['min'] . ":" . $_POST['sec'];

    if (date($format,strtotime($queryTime)) == $queryTime) {
        $now = new DateTime(strtotime($queryTime), new DateTimeZone('America/Los_Angeles'));
    } else { 
        echo "You entered:" . $queryTime . "<hr style='display:block;'>";
        exit;
    }

    $timezones = array(
        'EST' => new DateTimeZone('America/Panama'),
        'UTC' => new DateTimeZone('Europe/London'),
        'CCT' => new DateTimeZone('Asia/Hong_Kong'),
        'PST' => new DateTimeZone('America/Los_Angeles'),
    );

    echo "You entered:" . $now->format($format) . "<hr style='display:block;'>";

    foreach ($timezones as $zone) {
        $now = new DateTime($now, new DateTimeZone($zone));
        print "The time in" . $zone . "is" . $now->format($format) . "<br/>";
    }
?>

【问题讨论】:

  • 您希望用户输入月份为“May”还是“05”?

标签: php datetime timezone strtotime


【解决方案1】:

你的错应该被原谅 :D

说真的,我让你的代码工作了,看看这里的结果: http://sofia.bplaced.net/homework2.php

没有错。

我现在不知道这个页面有多可靠,但这显示了我所做的每一个更改: http://www.diffchecker.com/cfdm0ppc

新的工作代码:

<?php
$timezones = array(
  'EST' => -5*3600, //equador
  'UTC' => 0, //london
  'CCT' => +8*3600, //beijing
  'PST' => -8*3600, //los angeles
);

foreach ($timezones as $time) {
    $now = time();
    $now += $time;
    print gmstrftime('DATE: %B %d %Y and TIME: %r <br/>',$now);
}

    $nowM = date('m');
    $nowD = date('d');
    $nowY = date('Y');
    $nowHr = date('h');
    $nowMin = date('i');
    $nowSec = date('s');
?>

<form action="homework2.php" method="post">
    <label>Sync Timecode</label>
    <input type="text" name="month" placeholder="Month <?php $nowM ?>" value="<?php $nowM ?>" />
    <input type="text" name="day" placeholder="Day <?php $nowD ?>" value="<?php $nowD ?>" />
    <input type="text" name="year" placeholder="Year <?php $nowY ?>" value="<?php $nowY ?>" />
    <input type="text" name="hour" placeholder="Hours <?php $nowHr ?>" value="<?php $nowHr ?>" />
    <input type="text" name="min" placeholder="Minutes <?php $nowMin ?>" value="<?php $nowMin ?>"/>
    <input type="text" name="sec" placeholder="Seconds<?php $nowSec ?>" value="<?php $nowSec ?>"/>
    <input type="submit" id="button">
</form>

<?php
    $format = 'd-m-Y H:i:s';
    $queryTime = $_POST['day'] . "-" . date('m',strtotime($_POST['month'])) . "-" . $_POST['year'] . " " . $_POST['hour'] . ":" . $_POST['min'] . ":" . $_POST['sec'];

    if (date($format,strtotime($queryTime)) == $queryTime) {
        try {
               $now = new DateTime(date("c",strtotime($queryTime)));
            } catch (Exception $e) {
               echo $e->getMessage();
               exit(1);
            }
    } else { 
        echo "You entered: " . $queryTime . "<hr style='display:block;'>";
        exit;
    }

    $timezones = array(
        'EST' => 'America/Panama',
        'UTC' => 'Europe/London',
        'CCT' => 'Asia/Hong_Kong',
        'PST' => 'America/Los_Angeles',
    );

    echo "You entered: " . $now->format($format) . "<hr style='display:block;'>";
    foreach ($timezones as $zone) {
        $now = new DateTime(date("c",strtotime($queryTime)));
        date_timezone_set($now, timezone_open($zone));
        echo "The time in " . $zone . " is " . $now->format($format." \G\M\TP") . "<br/>";
    }
?>

我必须更改以下内容:

您的 IF 语句错误,您将 12 小时值与 24 小时值进行了比较 第 35 行:

"h" to "H" //altered hour-format

我预计您希望您的用户输入像“April”而不是像“04”这样的月份 第 36 行:

"$_POST['month']" to "date('m',strtotime($_POST['month']))" //altered month-format

您应该始终获取错误例外 - 这样您的代码可能会在出现错误时继续运行 第 36 行:

altered to "try{...}"

第 41 行:

"You entered: " //added a spacer

第 45 到 50 行:

modified the array as you tried to print these in line 56 and used them as parameter in 55

第 52 行:

"You entered: " //added a spacer

第 55 行:

"DateTime()" to "date_timezone_set()" //altered the command. The new cmd is part of the same function-set

第 56 行:

"print" to "echo" //I like uniform code

第 56 行(2 和 3):

"The time in " and "is " //added spacers

第 56(4) 行:

" \G\M\TP" //relative time to GMT

您写了一些关于“...显示相对于手动输入时间的 4 次中的每一次的时间”。 "\G\M\T" 将打印为 "GMT" "P" 将导致相对于 GMT$ 的时间

这应该适合你。
Aiyion.Prime

【讨论】:

  • 非常感谢您的详细回答,今晚晚些时候我会在家里复习。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多