0

I am using php and kohana framework.Below is the php code i am using to show the date and time.

$message_date = date('Y-m-d', strtotime($message->last_modified_date));
 $current_date = date('Y-m-d');
 $yesterday = date("Y-m-d", strtotime("yesterday"));
 if ($message_date == $current_date):
 $date = "Today at " . date('h:i A', strtotime($message->last_modified_date));
 elseif ($message_date == $yesterday):
 $date = "Yesterday";
 else:
 $date = date("M d, Y", strtotime($message->last_modified_date));
 endif; 



<span class="msg-date"><?= $date; ?></span>

Update: my own tried solution

$tz = new DateTimeZone('America/Chicago');
$message_date = new DateTime($message_date); 
$message_date->setTimeZone($tz);
$messagedate = $message_date->format('Y-m-d H:i:s');//converted time

I need to localize the time depends on the local machine the user is using.If the user viewing my page from "India" the time should adjust with Indian time,if the same page is viewed in USA or UK,the time should adjusted with USA or UK.

How can i do this in kohana or php.

user2681579
  • 1,413
  • 2
  • 23
  • 50

2 Answers2

0

You cannot do this server side (i.e. in PHP). A server can only read limited data from the client, which doesn't include the locale timezone.

However, you can use a mix of JS and PHP to do this, using an ajax call that sets a session for example - the same as explained in this question

Community
  • 1
  • 1
Nick
  • 6,316
  • 2
  • 29
  • 47
0

I believe that in PHP you're only able to use the time on the server-side. If you want to get time/data on the client side of the application, you should use JavaScript.

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10)
minutes = "0" + minutes

document.write("<b>" + hours + ":" + minutes + " " + "</b>")

Someone also struggled with this issue in this thread.

How can I get the user's local time instead of the server's time?

As mentioned earlier, you can use a mix of JavaScript and PHP.

Community
  • 1
  • 1
Saidin
  • 46
  • 4
  • please see my update, whether the solution taken by me is correct – user2681579 Feb 12 '14 at 09:43
  • You won't get the client-side time with that, since you set it to a certain country and you only convert the the timestamp to have a different notation, rather than the right time. Nick mentioned to use a mix between JavaScript and PHP. You can get the time stored in the session by that Javascript by using $_SESSION and store it inside your variable $messagedate. – Saidin Feb 12 '14 at 09:49