Strange issue in date in javascript and php - php

i m confuse in the date in javascript and php.
<?php
$mydate = date('2012-05-02 17:00:00');
echo 'Today PHP --'.$mydate;
$mytimstamp = strtotime($mydate);
echo '<br/>My PHP unix time stamp --'.$mytimstamp;
echo '<br/>'; ?>
<script type="text/javascript">
document.write('My Javascript unix time stamp --'+new Date(Number('<?php echo $mytimstamp;?>')*1000));
</script>
OUTPUT
Today PHP -- 2012-05-02 17:00:00
My PHP unix time stamp --1335978000
My Javascript unix time stamp --Wed May 02 2012 22:30:00 GMT+0530 (India Standard Time)
Why i m getting different time in javascript????

You are getting the same time, except in different time zones.
On PHP side you have a GMT time, and on JavaScript side you have India time.
It looks you are from India. Just make sure you understand the concept of time zones and you store the time with time zone information or in GMT/UTC time. This way you should avoid issues with incorrectly using timestamps from different time zones. Displaying such time in the form suited for user's time zone becomes trivial if you know the time zone in which it was generated.

if you do not change your timeZone than change your code like
<script type="text/javascript">
var d = new Date();
var offset = d.getTimezoneOffset();
document.write('My Javascript unix time stamp --'+new Date(Number(<?php echo (intval($mytimstamp));?> + offset*60 )*1000));
</script>

Your PC must have a different timezone set to that of the server running the PHP code.
The PHP date/time is calculated using the setting for the server whereas the JavaScript date/time is calculated using the clients PC settings.

In the javascript is depend on your browser. You would better decode date format on your javascript. You will get another different date on your different browser.

Related

What is the correct way to use PHP date/time with the jquery timeago plugin?

I'm using the yarp timeago plugin with Chicago CDT time on my server. THe problem i'm having is that it is showing different "ago" times on different PCs and in different timezones.
The PHP code im using to generate the UTC time is
$dateutc = gmdate('l F dS Y H:i',strtotime(''.$date.''));
Where $date is the timestamp of an item from the system in CDT (Chicago) time such as 2014-06-15 04:16:33, and $dateutc is supposed to be the UTC time.
The HTML to render the plugin output is
<abbr class=\"timeago\" title=\"$dateutc\"></abbr>
What am I doing wrong? Is there a better way to generate the UTC time in PHP?
Any help would be much appreciated.
I think it's just as simple as the format. Use this instead:
$dateutc = gmdate(DATE_ISO8601, strtotime(''.$date.''));

PHP Date formatting DST

I generate a unix-timestamp in the adminpanel of my site with a modified version of the jquery datetime-picker.
This timestamp get's saved into a mySQL database and in the frontend I output it via the strftime() function.
As the site has multiple admins and everybody began to add content, I encountered an offset of 1h in the output time, due to the timezone we're in (GMT+1). Because content was already added, I couldn't edit the function which generates the date and writes it to the db, so I had to edit the output function in the frontend.
I thought it would be clever to just add (myTime+3600) as an argument of the strftime function. Everything worked fine and more content got added.
Now the problem is, that all content assigned a date in april 2013 is an hour to late because of the Daylight saving time.
I only have the timestamp to work with so I somehow need to get whether a date needs the 1h offset or not. Any ideas on how to do that without adding a timezone at the timestamp generating function?
There is a function.
Any recommendations are welcome!
Example:
frontend-code:
strftime("%d. %B %Y / %H:%M Uhr",new_timestamp+3600);
For 13.March 2013 16:30 I get the correct output.
For 13. April 2013 16:30 I get 17:30
set the application default timezone using this http://php.net/manual/en/function.date-default-timezone-set.php that way regardless of what timezone the client is in it should force the timestamps into your primary timezone.
You may also need to edit the datetimepicker inits to force jquery to use the server based timezone too.

php code to get the timezone and the difference in current system time with GMT

Please help me with the php code to get the timezone and the difference in current system time with the London time
I need to get the time zone from where the mail is sent.After that i need to get the current system time as well as the GMT difference of current system time in php
get the current system time zone offset
get the the london time zone offset(from mail is sent)
London time zone : date_default_timezone_set('Europe/London');
PHP is server-side, and calling date() will give the time of the server.
You can pass different arguments to date() to get the information you need. date('Z') will give you the timezone offset in seconds (offset from UTC/GMT). You can also get it in hours and minutes using date('P').
To get the timezone of your local machine, you'll have to use something client-side, like Javascript. You could use jsTimezoneDetect to do this.
Give Javascript access to the server date, and compare the two dates:
var server_date = new Date(<?= date("YYYY-MM-DD") ?>);
var local_date = new Date();
You'll need to replace YYYY-MM-DD with a more appropriate time-and-date format string.
This blog and this PHP class should be enough for your needs
Tested function to get timezone difference between UTC and User's Timezone (Asia/Kolkata) can found
TimeZone Difference

PHP date() function not giving correct time

I am trying to figure out why php date() is giving me the wrong time, setting the actual time back 2 hours.
<?php echo date("Y-m-d H:i:s"); ?>
This gives 2011-01-01 03:14:04 instead of 2011-01-01 05:14:04. The hour is decreased by 2.
I have not change my timezone for date() and when users visit the site I want the time to be correct for their timezone also. How can I get this to work using php?
it is because by default it shows GMT time you can change for your region with following code
date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time
List of Supported Timezones
http://www.php.net/manual/en/timezones.php
You would have to use either date_default_timezone_set() or a datetime object, and the user would have to set their own timezone in an options menu somewhere.
Otherwise, PHP is a server side language and has no idea what time it is on the user's end.
You would have to use a client side language, JavaScript. You could either have it just be static and display the current user system time, or if for whatever reason you needed to get their time into PHP, you could use some AJAX like scripting to have JavaScript send their time into a script when the page loads.
Try setting the the timezone: date_default_timezone_set or via the ini
Update: you cannot set the correct date for your users. Javascript can handle it but you'd have to rely on the user's system to determine his/her time.
//Change date format
$dateInfo = date_parse_from_format('m-d-Y', $data['post_date']);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year']
);
$data['post_date']=date('Y-m-d',$unixTimestamp);

Converting UTS (UTC-0) times to BST in PHP or jQuery

I'm pulling the most recent listened tracks from last.fm and putting them on my website.
Problem is, the times are retrieved in UTC-0 uts format and appear to be an hour out when comparing them to BST times in order to calculate a fuzzy time stamp ("about 5mins ago", "about an hour ago" etc).
Is there any way solve this so the times always match BST/GMT and adjust when entering and leaving daylight saving time?
Here's a snippet of PHP code i'm using at the moment, which results in the times being an hour out.
$now = time(); // use this so all times are to the same second
$tz = getenv("TZ"); // save local setting so we can reset it later
putenv("TZ=Europe/London");
$trackPlayedAt = date('d M Y H:i:s', $track->date->uts);
date() automatically formats to the local timezone. The timezone depends on the configuration of the PHP server. If everything is set correctly, it should just work.
If you are running PHP 5.3 you have more options. Comment with which version of PHP you are running.

Categories