PHP Date Difference Current Time Tutorial

PHP Date Difference Current Time Tutorial

PHP Date Difference Current Time Tutorial

Today’s progress onto the start of yesterday’s PHP Date Difference Primer Tutorial is to allow the user …

  • pick a Timezone name (or hour offset from GMT) from left hand dropdown(s)
  • allow for a new option (off the hardcoded “Time” in that new dropdown) called “Is Currently” filling the date and time fields with relevant current date and time

… the methodology of making this happen showing the smarts of the same HTML iframe element in two guises, as …

  • target element of a form (method GET or POST) … or …
  • ahead of the submit button pressing to make above happen, an interim usage whereby reassign its “src” attribute with GET arguments facilitates …
    <?php

    /** Returns the offset from the origin timezone to the remote timezone, in seconds.
    * @param $remote_tz;
    * @param $origin_tz; If null the servers current timezone is used as the origin.
    * @return int;
    */
    function get_timezone_offset($remote_tz, $origin_tz = null) { // thanks to https://www.php.net/manual/en/function.timezone-offset-get.php
    if($origin_tz === null) {
    if(!is_string($origin_tz = date_default_timezone_get())) {
    return false; // A UTC timestamp was returned -- bail out!
    }
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    if (strpos(str_replace('GMT','/',str_replace('UTC','/',$remote_tz)), '/') === false) {
    $offset = (trim(str_replace('+','',urldecode($remote_tz))) * 60 * 60);
    if (strpos(('' . $offset),'-') !== false) {
    $origin_dt->sub(new DateInterval('PT' . str_replace('-','',('' . $offset)) . 'S'));
    } else {
    $origin_dt->add(new DateInterval('PT' . $offset . 'S'));
    }
    $rts=$origin_dt->format('Ymd:His');
    if (isset($_GET['rightnowstart'])) {
    echo "<html><body onload=\"parent.document.getElementById('datestart').value='" . $rts . "'; parent.document.getElementById('yyyydatestart').value='" . substr($rts,0,4) . "'; parent.document.getElementById('ommdatestart').value='" . substr($rts,4,2) . "'; parent.document.getElementById('ommdatestart').innerHTML='" . substr($rts,4,2) . "'; parent.document.getElementById('mmdatestart').value='" . substr($rts,4,2) . "'; parent.document.getElementById('odddatestart').value='" . substr($rts,6,2) . "'; parent.document.getElementById('odddatestart').innerHTML='" . substr($rts,6,2) . "'; parent.document.getElementById('dddatestart').value='" . substr($rts,6,2) . "'; parent.document.getElementById('ohhdatestart').value='" . substr($rts,9,2) . "'; parent.document.getElementById('ohhdatestart').innerHTML='" . substr($rts,9,2) . "'; parent.document.getElementById('hhdatestart').value='" . substr($rts,9,2) . "'; parent.document.getElementById('omidatestart').value='" . substr($rts,11,2) . "'; parent.document.getElementById('omidatestart').innerHTML='" . substr($rts,11,2) . "'; parent.document.getElementById('midatestart').value='" . substr($rts,11,2) . "'; parent.document.getElementById('ossdatestart').value='" . substr($rts,13,2) . "'; parent.document.getElementById('ossdatestart').innerHTML='" . substr($rts,13,2) . "'; parent.document.getElementById('ssdatestart').value='" . substr($rts,13,2) . "'; \"></body></html>";
    exit;
    }
    if (isset($_GET['rightnowend'])) {
    echo "<html><body onload=\"parent.document.getElementById('dateend').value='" . $rts . "'; parent.document.getElementById('yyyydateend').value='" . substr($rts,0,4) . "'; parent.document.getElementById('ommdateend').value='" . substr($rts,4,2) . "'; parent.document.getElementById('ommdateend').innerHTML='" . substr($rts,4,2) . "'; parent.document.getElementById('mmdateend').value='" . substr($rts,4,2) . "'; parent.document.getElementById('odddateend').value='" . substr($rts,6,2) . "'; parent.document.getElementById('odddateend').innerHTML='" . substr($rts,6,2) . "'; parent.document.getElementById('dddateend').value='" . substr($rts,6,2) . "'; parent.document.getElementById('ohhdateend').value='" . substr($rts,9,2) . "'; parent.document.getElementById('ohhdateend').innerHTML='" . substr($rts,9,2) . "'; parent.document.getElementById('hhdateend').value='" . substr($rts,9,2) . "'; parent.document.getElementById('omidateend').value='" . substr($rts,11,2) . "'; parent.document.getElementById('omidateend').innerHTML='" . substr($rts,11,2) . "'; parent.document.getElementById('midateend').value='" . substr($rts,11,2) . "'; parent.document.getElementById('ossdateend').value='" . substr($rts,13,2) . "'; parent.document.getElementById('ossdateend').innerHTML='" . substr($rts,13,2) . "'; parent.document.getElementById('ssdateend').value='" . substr($rts,13,2) . "'; \"></body></html>";
    exit;
    }

    } else {
    $remote_dtz = new DateTimeZone($remote_tz);
    $remote_dt = new DateTime("now", $remote_dtz);
    if (strpos($remote_tz, '/Hovd') !== false && strpos($origin_tz, 'GMT') !== false && $offset < 0) {
    $offset=-$offset;
    $remote_dt->add(new DateInterval('PT' . ('' . $offset) . 'S'));
    $remote_dt->add(new DateInterval('PT' . ('' . $offset) . 'S'));
    }
    $rts=$remote_dt->format('Ymd:His');
    if (isset($_GET['rightnowstart'])) {
    echo "<html><body onload=\"parent.document.getElementById('datestart').value='" . $rts . "'; parent.document.getElementById('yyyydatestart').value='" . substr($rts,0,4) . "'; parent.document.getElementById('ommdatestart').value='" . substr($rts,4,2) . "'; parent.document.getElementById('ommdatestart').innerHTML='" . substr($rts,4,2) . "'; parent.document.getElementById('mmdatestart').value='" . substr($rts,4,2) . "'; parent.document.getElementById('odddatestart').value='" . substr($rts,6,2) . "'; parent.document.getElementById('odddatestart').innerHTML='" . substr($rts,6,2) . "'; parent.document.getElementById('dddatestart').value='" . substr($rts,6,2) . "'; parent.document.getElementById('ohhdatestart').value='" . substr($rts,9,2) . "'; parent.document.getElementById('ohhdatestart').innerHTML='" . substr($rts,9,2) . "'; parent.document.getElementById('hhdatestart').value='" . substr($rts,9,2) . "'; parent.document.getElementById('omidatestart').value='" . substr($rts,11,2) . "'; parent.document.getElementById('omidatestart').innerHTML='" . substr($rts,11,2) . "'; parent.document.getElementById('midatestart').value='" . substr($rts,11,2) . "'; parent.document.getElementById('ossdatestart').value='" . substr($rts,13,2) . "'; parent.document.getElementById('ossdatestart').innerHTML='" . substr($rts,13,2) . "'; parent.document.getElementById('ssdatestart').value='" . substr($rts,13,2) . "'; \"></body></html>";
    exit;
    }
    if (isset($_GET['rightnowend'])) {
    echo "<html><body onload=\"parent.document.getElementById('dateend').value='" . $rts . "'; parent.document.getElementById('yyyydateend').value='" . substr($rts,0,4) . "'; parent.document.getElementById('ommdateend').value='" . substr($rts,4,2) . "'; parent.document.getElementById('ommdateend').innerHTML='" . substr($rts,4,2) . "'; parent.document.getElementById('mmdateend').value='" . substr($rts,4,2) . "'; parent.document.getElementById('odddateend').value='" . substr($rts,6,2) . "'; parent.document.getElementById('odddateend').innerHTML='" . substr($rts,6,2) . "'; parent.document.getElementById('dddateend').value='" . substr($rts,6,2) . "'; parent.document.getElementById('ohhdateend').value='" . substr($rts,9,2) . "'; parent.document.getElementById('ohhdateend').innerHTML='" . substr($rts,9,2) . "'; parent.document.getElementById('hhdateend').value='" . substr($rts,9,2) . "'; parent.document.getElementById('omidateend').value='" . substr($rts,11,2) . "'; parent.document.getElementById('omidateend').innerHTML='" . substr($rts,11,2) . "'; parent.document.getElementById('midateend').value='" . substr($rts,11,2) . "'; parent.document.getElementById('ossdateend').value='" . substr($rts,13,2) . "'; parent.document.getElementById('ossdateend').innerHTML='" . substr($rts,13,2) . "'; parent.document.getElementById('ssdateend').value='" . substr($rts,13,2) . "'; \"></body></html>";
    exit;
    }

    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
    if (strpos($remote_tz, '/Hovd') !== false && strpos($origin_tz, 'GMT') !== false && $offset < 0) { $offset=-$offset; }
    }
    return ($offset / 3600);
    }

    if (isset($_GET['rightnowstart'])) {
    get_timezone_offset(str_replace('+','_',str_replace('%20','_',urldecode($_GET['rightnowstart']))),'GMT');
    } else if (isset($_GET['rightnowend'])) {
    get_timezone_offset(str_replace('+','_',str_replace('%20','_',urldecode($_GET['rightnowend']))),'GMT');
    }


    ?>

but there’s more!

Think iframe, also think …

  • “onload” Javascript event functionality that might feed into “Client Pre-emptive Iframe” (suck it and see) ideas
  • “srcdoc” populate “entire HTML content” thinking

Again, feel free to try our changed “proof of concept” PHP web application.


Previous relevant PHP Date Difference Primer Tutorial is shown below.

PHP Date Difference Primer Tutorial

PHP Date Difference Primer Tutorial

When you think of Timezones, do you think of datetime differences?

Okay, well some people must … surely? Anyway, we do. We wonder, thinking about other people in the wooooorrrrllllddd, what time is it for them? Even, what day is it for them, the International Dateline blowing my tiny little mooooiiinnnnddd!

And so we wrote some PHP code to help with datetime differences whether they be related to Timezones or not. We designed a PHP web application that always starts with a defined “Start Datetime” and then allows either …

  1. End Datetime (deriving Datetime Difference) … or …
  2. Datetime Difference (deriving End Datetime)

… modes of use. Simple, huh?! And yet, even here, with that first scenario, a pretty fundamental thing was learnt, by yours truly, undertaking this exercise. A Datetime Difference result is only useful to the “Day” point because any one “Month” can be a different length of time to another, and so, becomes difficult to express in terms of a “Month Difference”.

Feel free to try our “proof of concept” PHP web application.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Not Categorised, Tutorials and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *