Question

Please forgive me, I'm just beginning to study PHP, but I'm pretty well stuck on this sample project that my tutor gave me to figure out.

The goal is to first display the local time of 4 preset timezones, then allow for the user to enter a time manually and on submit, display the time for each of the 4 times relative to the time that was manually entered.

It's also important to do a check that the user input is valid and not just pass any input on regardless of what it may be.

Something is not correct with my IF statement and no input matches, but I haven't been able to see the issue..

Would appreciate if anyone could point me in the right direction. I've been reading thru the documentation on PHP.net quite extensively and have read thru everything on dateTime already but perhaps I'm just not putting it all together yet.

<?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/>";
    }
?>
Was it helpful?

Solution

your fault shall be forgiven :D

Seriously, I got your code working take a look at the result here: http://sofia.bplaced.net/homework2.php

It wasn't THAT wrong.

I don't now how reliable this page is, but this shows every change i made: http://www.diffchecker.com/cfdm0ppc

The new working code:

<?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/>";
    }
?>

I had to alter the following things:

Your IF-statement was wrong, you compared a 12h Value to a 24H value line 35:

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

I expected you wanted your user to enter a month like "April" and not like "04" line 36:

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

You should always fetch error-exeptions - this way your code may go on in case of an error line 36:

altered to "try{...}"

line 41:

"You entered: " //added a spacer

line 45 ot 50:

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

line 52:

"You entered: " //added a spacer

line 55:

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

line 56:

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

line 56(2 and 3):

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

line 56(4):

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

you wrote something about "...display the time for each of the 4 times relative to the time that was manually entered." "\G\M\T" will print as "GMT" "P" will result in the time relative to GMT$

This should work for you.
Aiyion.Prime

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top