-1

When user enters Date and time using 'input type=date name="date"' && 'input type=time name="time"' tags according to his/her timezone,

for eg :- If a user from India, (Asia/Kolkata) timezone entered a date : 1/22/14 and time : 5:30pm , I need to Convert it into UTC time stamp for storing it to DB, To Achive that

I used below code:-

$year=substr($date,0,4);
$month=substr($date,5,2);
$day=substr($date,8);
$hour=substr($time,0,2);
$minute=substr($time,3);
$clientzone=mysql_result(mysql_query("select timezone from c_users_extra where  c_id='{$_SESSION['clientid']}'"),0,0); //Fetches the timezone of user
date_default_timezone_set($clientzone);//Setting default timezone to client timezone
$datetime = new DateTime("$year-$month-$day $hour:$minute:00");
$la_time = new DateTimeZone('UTC');
$datetime->setTimezone($la_time);
$values=$datetime->format('Y-m-d H:i:s');
$year=substr($values,0,4);
$month=substr($values,5,2);
$hour=substr($values,11,2);
$minutes=substr($values,14,2);
$seconds=substr($values,17,2);
$timestamp=mktime($hour,$minutes,$seconds,$month,$day,$year);//creating new timestamp from coverted 
print_r(getdate($timestamp));//Result :  Array ( [seconds] => 0 [minutes] => 30 [hours] => 6 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 )
//Expected Result : Array ( [seconds] => 0 [minutes] => 0 [hours] => 12 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 ) 

Why I get this wrong time stamp ?

Arun Wilson
  • 171
  • 2
  • 12
  • 2
    Sorry but that is such a huge overkill, why use all those `substr`s? When you can get those values directly – Hanky Panky Jan 22 '14 at 08:25
  • [offtopic]When I see getting year/month/day from date in PHP via `substr` I'm feeling myself so young...[/offtopic] Add desired results in your question – Alma Do Jan 22 '14 at 08:26
  • To retrieve the exact values from the date and time input tags.. – Arun Wilson Jan 22 '14 at 08:26
  • Just to make a timestamp? When the `DateTime` object [offers that functionality?](http://uk3.php.net/manual/en/datetime.gettimestamp.php) – George Jan 22 '14 at 08:27
  • need to convert User input date and time from users timezone to UTC timezone – Arun Wilson Jan 22 '14 at 08:30

4 Answers4

4

Here is an object oriented example:

<?php
  $date = '2014-01-22 18:15:00';  // assumed date is formatted correctly 
  $clientzone = 'America/New_York';  // assumed timezone is a valid one from your SQL
  $dateObj = new DateTime($date, new DateTimeZone($clientzone));
  echo "Original: " . $dateObj->format('Y-m-d H:i:sP') . "\n";

  $dateObj->setTimezone(new DateTimeZone('UTC')); // convert to UTC
  echo "Converted: " . $dateObj->format('Y-m-d H:i:sP') . "\n";
  echo "Epoch: ".$dateObj->format('U');
?>

You can format that just like the date function. The $date and $clientzone are assumed to be valid.

arikin
  • 188
  • 11
1

Can't you do something simpler like:

$user_time = strtotime($date);
$dateTimeZone = new DateTimeZone($timezone);
// get the offset from server time (UTC)
$tzOffset = $dateTimeZone->getOffset(new DateTime());
$result_time = $user_time - $tzOffset;
Alexander Nenkov
  • 2,910
  • 20
  • 28
1

Try following function to convert Local date time to UTC date time

function LocaltoUTC($date_to_convert)
{
    $local_timestamp = strtotime($date_t);

    $UTC_timestamp += ((-(5.5)) * 3600);  // 5.5 is UTC timezone or local timezone

    $gmt_datetime = gmdate('y-m-d H:i:s', $UTC_timestamp); 
    return $gmt_datetime;
}
  • Ugly solution using unix timestamps and hard-coded timezone offset and makes no allowance for daylight savings rather than using the DateTime and DateTimeZone objects – Mark Baker Jan 22 '14 at 09:10
  • @Mark Baker - hard coded timezone is just for a sample. Developer has to put up own logic to get timezone. I'm not claming that my solution is ready to use, but as I said to try it and make it batter – Trimantra Software Solution Jan 22 '14 at 09:14
0

Here is a similar question. Take a look at Adjusting time zone in PHP with DateTime / DateTimeZone

There is a solution and a more comfortable way to do your task.

Community
  • 1
  • 1
createproblem
  • 1,542
  • 16
  • 30