Hour in time not outputting correct - php

I am using Wanp as a testing server. Here is my code:
$dt = time();
$mysql_datetime = strftime("%Y-%m-%d %H:%M:%S %p %z" , $dt);
echo $mysql_datetime;
Here is the output:
2011-04-17 01:36:55 PM Eastern Standard Time
My problem is that, all the information is correct except or the hour, which is 4 hours ahead. The hour (01 PM) is suppose to be (09 AM). I have checked my date and time on my computer, and the time zone is correct. What would cause this to happen, please help.

Chances are this is due to the timezone being set incorrectly. (Although it's odd that the %z is correct, which I'm presuming it is.)
Have you tried calling date_default_timezone_set with the appropriate timezone value?

You have to set your timezone correctly first before anything else.
Use date_default_timezone_set. Here is list of supported timezones.

Could you try using the DateTime class (assuming you have a reasonably recent php)?
Something like:
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s A e');
Alternatively if it still gets the timezone wrong, you could try to specify it:
$dt = new DateTime(null, new DateTimeZone('EST'));
echo $dt->format('Y-m-d H:i:s A e');
Anyway for more debugging information I'd recommend you look at ini_get('date.timezone') which, if incorrect, should be configured properly in your php.ini file.

Related

Setting PHP DateTime doesn't appear to be observing TimeZones

I'm fairly new to PHP so forgive me if this is a stupid mistake that I haven't spotted
I've run into a problem where in our current system where we currently used strtotime and it was returning our date an hour ahead than it actually was set. E.g 1:15pm became 2:15pm when I set the timezone to be European rather than GMT.
I read that strotime had this problem but I can't get it to observe a different timezone if I try and set it.
So I tried working with PHPs DateTime instead.
The user enters the time and they select it as 1:15PM however we want to store it as 13:15. So I did this:
$t = DateTime::createFromFormat('h:i A', $venue['startTime']);
$t_24 = $t->format('H:i:s');
Then I try and create my Date object
$d = DateTime::createFromFormat('d-m-Y H:i:s', $venue['startDay'] . ' ' . $t_24);
$d->setTimezone(new DateTimeZone('America/New_York'));
echo ' ' . $d->getTimestamp();
Trying to set the timezone after the object is set because apparently it doesn't work if you add the timezone as the third argument in createFromFormat
My computers time is currently observing European time currently GMT+1 because we're observing daylight savings time in the UK, I select the time set on the through our system as 1:15pm and because I've set the timezone I expect the timestamp outputted equivalent to 7:15am as it's six hours behind European time, however when I convert the timestamp 1500639300 it's still equal to 2:15 PM. Probably done something stupid but can't quite figure out what? Any help would be appreciated :)
Timestamps have no time zone - they are always in UTC time. If you want to save timezone related data use another format! For example save in H:i:s, as you need it.
you can use gmdate() Function for this
<?php $current_time = gmdate('Y-m-d H:i:s'); ?>
http://php.net/manual/en/function.gmdate.php

PHP date showing wrong time despite the timestamp being correct

I'm having a problem with the PHP date function which I've never had a problem with before.
The timestamp is entirely correct, however for some bizarre reason date() is outputting a time which does not correspond.
I have the following timestamp (and this is definitely the correct one - when I echo it out onto the page, as well as in the database, it shows as being correct):
464400
Yet when I use the following line of code:
<?php echo date("H:i",$timestamp); ?>
I'm getting a time of 4 am? If I paste the timestamp into a timestamp converter website, then it shows the time should in fact be 9am.
I'm completely stuck, this has never happened to me before & this problem has only just come up recently - the code hasn't been changed and everything seemed to be working correctly before.
Does anyone have any experience with this issue? Any help would be appreciated.
That time stamp is is 9am GMT timezone, if you are in another timezone then you will need to adjust it accordingly.
http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('Europe/London');
or even better in your php.ini
http://php.net/manual/en/datetime.configuration.php
date.timezone="Europe/London"
Or you can use more specifically GMT instead of Europe/London (which has DST)
try this method will work
for time zone
http://php.net/manual/en/timezones.php
code
<?php
date_default_timezone_set('Asia/Kolkata');
$dt2=date("Y-m-d H:i:s");
echo $dt2;
?>
try this
// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
Try this converter too http://freeonlinetools24.com/
For time zone: https://www.php.net/manual/en/timezones.php
date_default_timezone_set('America/Chicago');
echo date("m/d/Y h:i:s A");

Why is DATE_FORMAT() in php a few hours behind?

The form I am using to submit data to the database is processing the time incorrectly (usually a few hours behind the actual time). The output below was really done at 9:28 not 7:28. How do I fix this so that it processes the time correctly? I do not want to use military time. Is it possible that it could be something with my website hosting service? I tried this on XAMPP and everything works fine. Any help would be greatly appreciated.
Code:
DATE_FORMAT(posts.post_date, '%W, %M %e, %Y at %h:%i %p') AS date
Output:
Saturday, July 6, 2013 at 07:28 PM
The time would go by your server time. An easy workaround for this is to manually set the timezone before the date() or time() functions are called to.
I'm in Kolkata, India so I have something like this:
date_default_timezone_set('Asia/Kolkata');
Or another example is LA - US:
date_default_timezone_set('America/Los_Angeles');
You can also see what timezone the server is currently in via:
date_default_timezone_get();
So something like:
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
So the short answer for your question would be:
// Change the line below to your timezone!
date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y h:i:s a', time());
Then all the times would be to the timezone you just set :)
As I commented above, you can set the default timezone for your dates and times using
date_default_timezone_set(). The documentation can be found here.
The list of supported timezones can be found here. You can pass your default timezone as a parameter in the aforementioned method.
If you turn E_NOTICE on, you'd probably work this one out as it chucks up notices if you use date/time functions without setting the time zone.
bool date_default_timezone_set ( string $timezone_identifier )
E.G:
<?php
date_default_timezone_set('America/Los_Angeles');
http://www.php.net/manual/en/function.date-default-timezone-set.php

Changing PHP default timezone not working

I am trying to have a line of code added to an html document that is preceded by the time. I want the time zone to be relative to me, however I cannot change it from the default UTC. I have changed in the php.ini file to PST as well as using date_default_timezone_set('America/Los_Angeles'); and yet it still prints the time 7 hours ahead of my timezone. Heres the code that deals with the time:
session_start();
if(isset($_SESSION['name']))
{
date_default_timezone_set('America/Los_Angeles');
$msg = $_POST['text'];
$fo = fopen("log.html", 'a');
fwrite($fo, "<div class=msgln>(".date("g:i A").") <b style=color:red;>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($msg))."<br></div>
");
fclose($fo);
}
Servers should be set to UTC, and you should not be looking to change the default. Instead, what you want to do is create a DateTime object based on the time, then convert it to the timezone you want, and display.
$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $now->format('g:i A');
I don't know if your format string is valid or not, but the format method is suppossed to be compatible with that accepted by the date() function you were using in your original example.
First make sure you're using a valued timezone. You can find a list of supported timezones in the PHP docs.
The second problem is using date() without specifying the timestamp. This defaults to the timestamp produced by time() which (based on a comment in the documentation) is UTC time. You'll either have to use strftime() or manually subtract the difference from UTC.
If you use 'etc/GMT' you can set the dateTime object to the desired time zone like so:
$dtz = new DateTimeZone('etc/GMT-10');
$dt = new DateTime(date("Y-m-d h:i A"), $dtz);
$date = gmdate("Y-m-d h:i A", $dt->format('U'));

In the format of date and time in php, the timing is not correct

When I am fetching the date n time using this function:
date("d-M-Y h:i A");
It gives the wrong timing.
The current correct timing is 04.30 pm but my function is showing 11.00 am.
That means showing 5.30 hours early.
help me out to fetch the correct timing.
Going by your username, I think you're from India.
So put date_default_timezone_set('Asia/Kolkata');
right before date("d-M-Y h:i A");
OR
$offset= strtotime("+5 hours 30 minutes");
$date = date("Y-m-d H:i:s",$offset);
There are two probable causes.
1) The timezone.
2) The server time.
These are actually related.
Check the server time (ie the system time on the machine running the code). If it's incorrect; fix it. If it's correct, then check the timezone also and reference it against the default timezone on the php side.
If working on localhost, change your time zone in your php.ini file.
OR try folowing way:
<?php
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
echo $date->format( 'H:i:s A / D, M jS, Y' );
?>
It seems that you are getting the UTC time instead of your timezone (which is +5:30 from GMT). (This could be because your default timezone could be set to UTC)
Try this:
date_default_timezone_set('GMT');
$temp= strtotime("+5 hours 30 minutes");
$date = date("Y-m-d H:i:s",$temp);
echo $date;

Categories