PHP Date formatting DST - php

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.

Related

Issue in time-stamp date in php

I am having trouble in the timestamp.
When I am using the LAMP in Ubuntu, then it works with correct date which I entered, but on other systems it show 1 day back's date.
I don't know what I need to do now. I have stored the timestamp in my database. But when I am showing it on my web application, it works fine in the LAMP but not in others.
When I am converting the timezone to online converter it shows backdated result. What do I do now?
You can set PHP default timezone before reading the date from the timestamp.
Add the following line before reading the date.
date_default_timezone_set('Asia/Calcutta');
Let me know if this helps.
References:
http://php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/timezones.php
The timestamp stored is correct, Please set the default time zone in your PHP application to let the system know which timezone you are using then it will store the correct time zone. the below link would help you.
I am assuming you are storing timestamp in DB and retrieving it to display
http://php.net/manual/en/function.date-default-timezone-set.php
Thanks

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

how to make all users on site share a universal timezone using php or javascript?

I'm using this great jQuery timepicker http://trentrichardson.com/examples/timepicker/
It gives a string something like 07/20/2011 01:13 am
I have this situation where users on the site can schedule "events" and they are using this to pick the event. But im realizing that it could cause a lot of confusion with all the different timezones that people will be submitting from.
So I was thinking I could use php to somehow get the timezone, and the adjust how the viewer views the dates according to their timezone. But this seems like quite a task and was wondering if there was an easier way?
Also, even with the php idea I tried echo date_default_timezone_get(); as a test to see if it got my timezone, but its saying im in the Los Angelas timezone, rather than Baltimore/DC timezone.
I'm guessing its getting the timezone of my server since php is serverside.
So basically, whats the best way while using my time picker jquery script, that I can have all the users view the date/times adjusted to their timezones? Any help would be greatly appreciated.
when you get the datetime from user, save in a default timezone (GMT 0 for example), you need to know the user timezone.
When you get the datetime from DB, add the user timezone to it.
You are right, the default timezone is the server's.
get user timezone
you can save it in cookie
The term is UTC Time, Universal Coordinated Time. Googling that will find you a lot, in the meantime:
http://www.w3schools.com/jsref/jsref_getutcdate.asp
Direct give the UTC time (e.g. "Wed, 20 Jul 2011 06:10:38 GMT") to the users and use JS to convert it to their local times.
var localTime = new Date("Wed, 20 Jul 2011 06:10:38 GMT");
alert(localTime); // shows local time like: "Wed Jul 20 2011 14:10:38 GMT+0800 (CST)"

php date.timezone problem

i am storing member's created date using the time() function, and echo them back using strftime() and the date() function, but it doesnt display the right time information
my code is :
date("d-m-Y H:i:s", $member->created)
strftime("%Y-%m-%d",$member->created)
it should have displayed this:
17-11-2010 14:42
rather than:
17-11-2010 23:45
now, i have change my php.ini date.timezone configuration into my current time zone which is "Asia/Jakarta", yet nothing change. i have also try to set them manually inside my code using
date_default_timezone_set("Asia/Jakarta");
what did i miss ? please help me
Regards
EDIT:
according to Noodles, i need to check the server's clock setting, so i did using the
hwclock command, and it turns out that the clock setting is not right, since it displayed
Thu 18 Nov 2010 02:07:25 AM WIT -0.931691 seconds
while it should have been
Wed 17 Nov 2010 05:04:ss PM
now, how can i syncronize this setting into my timezone ??
Edit: i have manage to change the clock setting using
date -s "HH:MM:SS"
hwclock --systohc
thanks for pointing me out Noodles
case is closed i guess :D
thanks again guys
Are you sure your server's clock is set correctly? It would seem weird that the minutes would change. At the most I'd expect the hours to change if it was a different time zone, but not minutes.

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);

Categories