请原谅我,我刚刚开始学习PHP,但我很好地坚持这个示例项目,我的导师让我弄明白。
目标是首先显示4个预设时区的本地时间,然后允许用户手动输入时间并提交,显示相对于手动输入时间的4次中的每一个的时间。 / p>
检查用户输入是否有效并且不管是否输入任何输入都是很重要的。
我的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/>";
}
?>
答案 0 :(得分:2)
你的错将被原谅: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声明错误,您将12h值与24H值进行了比较 第35行:
"h" to "H" //altered hour-format
我希望您希望您的用户输入一个月份,例如&#34; April&#34;并不喜欢&#34; 04&#34; 第36行:
"$_POST['month']" to "date('m',strtotime($_POST['month']))" //altered month-format
您应始终获取错误消息 - 这样您的代码可能会在出现错误时继续 第36行:
altered to "try{...}"
第41行:
"You entered: " //added a spacer
第45行ot 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
你写了一些关于&#34; ...显示相对于手动输入的时间的4次中的每一次的时间。&#34; &#34; \ G变\中号\ T&#34;将打印为&#34; GMT&#34; &#34; P&#34;将产生相对于GMT $
的时间这对你有用。
Aiyion.Prime