1,105,363 Community Members

Convert unix time to local time.

Member Avatar
ckjaseem
Newbie Poster
7 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi ,

Can anyone tell how I will send the local time zone to the server and so that I can convert the unix time(coming from the database) to the local time and display it on the clients side using php (In short I dont want to use javascript when I display the time)

This is how I saved the time to database: -

Javascript

var exp_day = $("#exp_day").val();
if ( (exp_day == 0) || (exp_day == "") ) { exp_day = 00; }
else { exp_day= parseInt($("#exp_day").val()); }

var exp_hrs = $("#exp_hrs").val();
if ( (exp_hrs == 0) || (exp_hrs == "") ) { exp_hrs = 00; }
else { exp_hrs= parseInt($("#exp_hrs").val()); }

var exp_min = $("#exp_min").val();
if ( (exp_min == 0) || (exp_min == "") ) { exp_min = 00; }
else { exp_min= parseInt($("#exp_min").val()); }

var exp_time = ( (exp_day*24*60*60) + (exp_hrs*60*60) + (exp_min*60) ) ; // converting to seconds

I then posted the exp_time to a php file where it is saved into the database in unix time

php

$exp_time = time() + $_POST["exp_time"]; 

$exp_time is saved to the database.

?? Now what I need to do is , displaY this $exp_time (UTC- unix time; which is a future time) on to my clients machine. It should be display according to his local time takeing into consider his dayalight saving and all.??

what I want to do is , when the client logs in I need to grab his local time and send it to the server. So that I can do the conversion in server and display the time using php.

I have solved this problem using javascript but I was told this is not a good way :
php file which displays on the client side

<script>
function callMe(x,y) {
    var theDate = new Date(x*1000);  // convert time value to milliseconds
    var dateString = theDate.toLocaleString();
    var hours = theDate.getHours();
    var minutes = theDate.getMinutes();
    var ampm = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    var strTime = hours + ':' + minutes + ' ' + ampm;
    var monthNames = [ "Jan", "Feb", "March", "April", "May", "June",
    "July", "Aug", "Sept", "Oct", "Nov", "Dec" ];
    var year = (theDate.getFullYear() + '').substring(2, 4);
    document.getElementById(y).innerHTML = "Exp @ "+strTime+" "+monthNames[theDate.getMonth()]+ " " +theDate.getDate()+"/"+year;
}
</script>

<div id='<?php echo $needTime[$i]; ?>' class='cpc_lower'></div></div>

<?php
echo "<script type='text/javascript'>callMe(".$Data['exp_time'].",".$needTime[$i].")</script>";
?>

This is how I tryed to solve my problem but this is not an effeciant way. So can anyone please tell me what should I send to the server (timezoneofset or something ) , so that I can do convertion in php and display it on the client. It will be greatly helpfull if you can give a coded example.

LastMitch
Deleted Member
 
1
 

@ckjaseem

In short I dont want to use javascript when I display the time

You this:

http://phpmaster.com/working-with-dates-and-times/

It will save you alot of time. Plus, you will get a chance to see how it works.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: