In PHP, how do I get the current time, in UTC, without hard coding knowledge of where my hosting provider is?
For example, I tried the following:
time() + strtotime('January 1, 2000')-strtotime('January 1, 2000 UTC')
and find that it reports a time that is one hour ahead of actual UTC time. I tried this on two different hosting providers in two different time zones with the same results.
Is there a reliable (and, hopefully, cleaner) way to accurately get the UTC time?
I am limited to PHP 4.4.9 so I cannot use the new timezone stuff added to PHP5.
Thanks, in advance.
$time = new DateTime('now', new DateTimeZone('UTC'));
echo $time->format('F j, Y H:i:s');
This seems to work for me. Of course, you'll need to test it on PHP 4 since all of my servers have PHP 5, but the manual claims this should work for PHP 4.
$t = time();
$x = $t+date("Z",$t);
echo strftime("%B %d, %Y # %H:%M:%S UTC", $x);
First time around, I forgot that the date could change between the call to time() and date().
Does this work for php 4.4.9?
echo gmdate('Y-m-d H:i:s', time());
or if you want it for a specific date:
$time = strtotime('January 1, 2000 UTC');
if($time){
echo gmdate('Y-m-d H:i:s', $time);
}
$utcTtime = gmmktime();
$unixTimestamp = time();
gmmktime: Get Unix timestamp for a GMT date
Related
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;
I need to convert a date and time (GMT) into a timestamp with php. The following code shows what I'm currently using:
<?php
$date="2012-06-29 10:50";
$timestamp = strtotime($date);
echo $timestamp;
?>
However, when I test the timestamp in an online convertor (http://www.epochconverter.com), the resulting date is 29th June 2012, 8:50 GMT, or 2 hours previous. Is it possible that the strtotime() function isn't completely accurate and is just an estimate of the time? If so, are there better methods I could use for getting the exact time?
Thanks.
strtotime assumes that you are converting a string in your server's local time, so if the servers time zone is two hours out the result will be as wll.
The comments in the manual suggest a couple of solutions, you can append UTC to your date:
$timestamp = strtotime($date.' UTC');
Or you can change the default timezone for the script (this will apply to all other time functions!):
date_default_timezone_set('UTC');
$timestamp = strtotime($date);
As a final alternative, you could try date_create_from_format which allows you to specify what exactly the format your string is:
$datetime = date_create_from_format('Y-m-d H:i', $date, new DateTimeZone('UTC'));
$timestamp = date_format($datetime, 'U');
// Alternatively (thanks Herbert) - 5.3+ only
$timestamp = date_timestamp_get($datetime);
I am trying to create a select list starting from the current date of the user. I want it so that it is set to midnight in unix timestamp format.
This is all I'm doing:
$today = strtotime('today');
echo $today;
This is my result:
1333144800
which is: Fri, 30 Mar 2012 22:00:00 GMT according to Epoch Converter (incorrect by a couple hours.)
If you want strtotime() to return a timestamp relative to UTC (00:00:00 UTC instead of e.g. 00:00:00 UTC+2, if your system is set to a timezone with an offset of 2 hours against UTC/GMT), you need to specify that:
$today = strtotime('today UTC');
GMT (+0) time
echo $today = strtotime('today GMT');
echo "<br>" . $today = date("d-m-Y H:i:s", $today);
We expect that your server runs at GMT - that is the best (for maneuvering with time displays later). If not, you MUST adjust php.ini set this "date.timezone = GMT".
When you get that done, you will see 00:00 with my codes.
Then, you must develop function (ie DisplayDate()) in your script to display dates of your website correctly if
youre not in GMT area
or/and if you want for your users to see times in their timezone with timezone selection for example.
DisplayDate() should include support for daylight changes also (0, or +1 hour / summer and winter time).
strtotime( $time )
is designed to return a unix timetamp, meaning, it will return the number of seconds since jan 1, 1970. http://www.php.net/manual/en/function.strtotime.php
To get around this, use something like:
$today = date("d/m/Y H:i:s", strtotime('today'));
echo $today;
You might have to specifically set the time as well as the day:
$today_midnight = strtotime('today UTC 00:00');
You should check the timezone configuration in your php.ini file. In my case (I live in El Salvador) I had to change it like this:
date.timezone = America/El_Salvador
I'm trying to output the current date and time using this code:
$theDate = date('y-m-d H:m:s', time());
echo $theDate;
And it works fine but the output for time does not change minutes, it simply sites at HH:07:SS, so the minute sits at 07 and the only thing that changes is the seconds and hours.
Is this because of the time function inside PHP? Does it only update minutes so often? Why would it not update the minutes as well?
How can I get an output the same but with minutes showing correctly?
Whenever i run strftime on the server it outputs fine, just trying to figure it out above.
Use i not m:
$theDate = date('y-m-d H:i:s'); echo $theDate;
07 is July :)
m represents months, not minutes. You need to use i for minutes. See the date() manual page for more information.
$theDate = date('y-m-d H:i:s'); echo $theDate;
Your format string is wrong.
It could be: y-m-d H:i:s
You dont need to give date function the second parameter time().
Just try date("format string");
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.