0

I have almost one year programming experience in PHP, but always I'm facing a greate challenge of capturing spesific Time when user submit a post into a forum. Simply I have created MySQL Database table which store date, Time, and user coment, But when user submit a coment to the database everything entered correctly but only Time value goes differently from what I expect. Here what I want is to store exactly the same time value as that of the local client machine shows.

Hey guys if any one knows how to do it please help me for that.

Thank you.

5
Contributors
4
Replies
31
Views
3 Years
Discussion Span
Last Post by diafol
0

I don't think it is possible using PHP since PHP is server side only. It will only return the time and date of the server. You may need to use javascript or another client side language to get the client machine time and then capture that value with PHP and insert it into your database.

Edited by Banderson

0

You can save the datetime as UTC in the posts table, and then convert it to the user timezone, basing on his profile preferences. For example:

<?php

    $times  = DateTimeZone::listIdentifiers();
    $utc    = key(array_slice($times, -1, 1, TRUE));
    $getTZ  = array_key_exists('tz', $_GET) ? (int)$_GET['tz'] : $utc;

    # generate list of timezones
    $list = array();
    foreach($times as $key => $value)
    {
        if($key == $getTZ)
        {
            $list[] = "<option value=\"{$key}\" selected>$value</option>";
        }

        else
        {
            $list[] = "<option value=\"{$key}\">$value</option>";
        }
    }

    $dt     = new DateTime('now', new DateTimeZone($times[$utc]));
    $date   = $dt->format('Y-m-d G:i:s');

    $dt->setTimezone(new DateTimeZone($times[$getTZ]));
    $converted_date = $dt->format('Y-m-d G:i:s');

    echo "<p><strong>Default:</strong> UTC {$date}</p>";
    echo "<p><strong>Converted:</strong> {$times[$getTZ]} {$converted_date} </p>";


?>

<form method="get" action="">
    <select name="tz">
        <?php echo implode("\n", $list); ?>
    </select>
    <input type="submit" name="submit" value="update" />
</form>

This will generate this kind of output:

Default: UTC 2014-07-11 14:20:29
Converted: Pacific/Funafuti 2014-07-12 2:20:29

Now, when you retrieve the datetime column from the posts table use something like:

$times = DateTimeZone::listIdentifiers();
$userTZ = $_SESSION['user_timezone'];

$dt = new DateTime($row['date'], new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone($times[$userTZ]));
echo $dt->format('Y-m-d G:i:s');

In this example the timezone is set in the session, so when the user logs in. In order to work you can create a column timezone in the users table and save her/his preference. Use the array created by DateTimeZone::listIdentifiers() for this operation.

For more information check the documentation:

Bye!

1

The user time should not be considered IMO. Use server time or as suggested set a universal Timezone for logging posts (preferrably store datetime as a unix timestamp). If a user has a profile, you can ask them to choose a timezone of their choice so that all post datetimes are displayed in their preferred timezone setting. This obviates messing about with most date functions and sending client-side data to the server.

8b0c55811f0eca28fd53692158ccf0ee

(Example from Daniweb Profile 'Member Options')

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.