How to get local time in php? - php

I am trying to get the local time using php. I wrote two different versions, but they both give the wrong time
date_default_timezone_set('UTC');
$now = new DateTime();
echo $now->getTimestamp();
Another way
date_default_timezone_set('America/New York');
echo strtotime("now")."<br/>";;
$now = new DateTime();
echo $now->getTimestamp();
In both cases I get the time 4 fours ahead of my local time. There is any other way to get the local time?

DateTime::getTimestamp() returns unix timestamp. That number is always UTC. What you want is format the date according to your time zone.
$dt = new DateTime("now", new DateTimeZone('America/New_York'));
echo $dt->format('m/d/Y, H:i:s');
Or use a different date format, according to what you need.
Also, you can use DateTime library for all your date needs. No need to change server's default timezone every time you want to fetch the date.

Simply use function date_default_timezone_set(). Here is example:
<?php
date_default_timezone_set("Asia/Dhaka");
echo date('d-m-Y h:i:s A');
?>
Hope it will help,
Thanks.

You need to use javascript because you want the value of time from the client. PHP is a server-side language that evaluates on the server and sends results to the client. So if you try to pull a date/time using a PHP function, it's going to pull the date from the server, which is not always in the client's timezone.
You can do this in a javascript function and then reference the value in the display.
var thisDateTime = new Date();

If you don't want to use timezone you can use this mix of windows and php commands:
<?php
$D = exec('date /T');
$T = exec('time /T');
$DT = strtotime(str_replace("/","-",$D." ".$T));
echo(date("Y-m-d H:i:s",$DT));
?>

I'm wondering why nobody has mentioned localtime(time()); in PHP with indexed key array result or localtime(time(), true); with associative key array result.

For the record, I found the only solution to read the real hour of the machine is to read the information outside of PHP (javascript, shell or other processes). Why?
For example, let's say we have an hour based in daily-saving. What if the timezone of the OS (Windows in my case) is not in sync with the timezone of PHP, I mean, it could be calculated differently. Or maybe the machine is using a different hour and it is ignoring the daily-saving hour.
Right now, my zone is UTC -4 Santiago 9:35 pm (with daily saving). However, PHP considers as 10:35 PM (using localtime,time,date and DateTime).

If anyone here wanted to get the local time according to the user's timezone (dynamically), then you can consider the following code:
$ip = $_SERVER['REMOTE_ADDR'];
$ipInfo = file_get_contents('http://ip-api.com/json/' . $ip);
$ipInfo = json_decode($ipInfo);
$timezone = $ipInfo->timezone ?? "UTC";
$dt = new DateTime("now", new DateTimeZone($timezone));
echo $dt->format('Y-m-d H:i:s');
The above code will get the user's local timezone and print the time according to that.
If you wanted to test the above code on your localhost then make sure to hard code your IP address there because the API can't provide you the timezone based on your local address (127.0.0.1), but the above code works fine on the
live server.
For Localhost, do:
$ip = '-------'; //your IP address

You can solve this problem by using localtime() function or through date_default_timezone_set() function.
Learn more about the localtime() function reffer:
http://php.net/manual/en/function.localtime.php
or
Learn more about the date_default_timezone_set() function reffer http://www.php.net/manual/en/function.date-default-timezone-set.php
i think this must help you..

Related

Converting from EDT to GMT+05.30 time zone in php for entering timestamp in mysql database

My php web servers timezone is EDT (Eastern Day Light Time (US)). My current timezone is GMT+05.30. I need to enter current timestamp into my mysql database with the timestamp in my current timezone. By the way, I'm using a free php web server for my use. So I will not be having any previleges for modifying the server. Can some one suggest me some way of converting it to my GMT+05.30 from EDT in php using any script.Thanks in advance.
Have a look at the following example
$timezone = new DateTimeZone('Europe/Berlin');
$date = new DateTime('#' . $yourTimestamp, $timezone);
echo $date->format('c');
So through the DateTimeZone Object your Time will be formatted by the DateTime object.
I suggest you use the date_default_timezone PHP function.
You can read about it here and here is the list of Supported Timezones
Example:
<?php
date_default_timezone_set('Europe/Amsterdam');
?>
Use this:
echo gmdate("Y:F:l h:i:s",time()+19800);

Manage PHP time function

I am using godaddy hosting service and I can manage local time, I have to use the server default time that is America/Phoenix.
Even if i am using date_default_timezone_set("Asia/Kolkata");
function in my config file then also there is no difference in time and godaddy people are not ready to help me with, I am tired of calling this guys but no response, I hate them all.
Is there any means I can get my local time using any function or any external API?
I am using this code
//set time zone india
date_default_timezone_set("Asia/Kolkata");
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
$date = date('m/d/Y h:i:s a', time());
echo $date."<br>";
and the output that I am getting is
The current server timezone is: Asia/Kolkata05/14/2014 12:06:26 am
Even if it is 4:38 pm here...
date_default_timezone_set should work for PHP functions like date, however I'm going to hazard a guess that you're having problems with date/time elements in a database such as MySQL.
I know I've had similar problems before, when trying to get everything on UTC instead of Europe/London...
When you've established the connection to your database, be sure to run this query:
SET time_zone = 'Asia/Kolkata';
This, in addition to date_default_timezone_set, should solve your problems. However, if you're using DATETIME columns, then those will not be fixed. TIMESTAMP columns will be automagically fixed to the new timezone because they are saved as UTC internally and converted upon retrieval.
You shouldn't touch date_default_timezone_set. The proper way to do that would be to use DateTimeZone object. Something like this:
$now = new \DateTime('now', new \DateTimeZone('Asia/Kolkata'));
echo $now->format(\DateTime::ATOM);
So, the idea is that you create an object in a timezone of server and then convert it to your timezone

Php Date function not working in live server

i am displaying the current date & time using php in localhost using below date function
<?php
echo date('Y-m-d H:i:s');
?>
and displaying output as 2013-06-24 14:45:13
But when i run the above php code in the live server it is displaying output as 2013-06-24 06:47:28.
So, any one tell how could display the date and time in 24 hour format in the live server..
This is a timezone issue.Check
date_default_timezone_get();
Using date_default_timezone_set('YOUR_TIMEZONE'); you can set timezone.
Seems a timezone issue or settings not properly set on server as theoretically it should display time and date as of server
Regarding Timezone issue, see this guide for date_default_timezone_set
http://php.net/manual/en/function.date-default-timezone-set.php
Your server seems not to have the same timezone as your localhost.
Or maybe the time is simply not well set.
The date function takes the current date and time. Now it will display only the date and time will be displayed that is at server.
Use DateTime:
<?php
$date = new DateTime('now');
echo $date->format("Y-m-d H:i:s");
?>
Also, the date/time displayed is server's one, so you should also set the timezone:
<?php
$timezone = DateTimezone('{Continent}/{City}');
$date = new DateTime('now', $timezone);
echo $date->format("Y-m-d H:i:s");
?>
When you are running this code echo date('Y-m-d H:i:s'); in localhost it is taking your system time but when you run this echo date('Y-m-d H:i:s'); on live server it uses the SERVER Timezone. Use this function date_default_timezone_get(). It returns the default timezone used by all date/time functions in the script and you can use it to set the time zone.
You can
date_default_timezone_set('America/Los_Angeles');
or
date_default_timezone_set('Egypt/Cairo');
or what ever between ('')
Use date_default_timezone_set to sent corrent time zone from your script.
You can find more info here: http://php.net/manual/en/function.date-default-timezone-set.php

PHP Date from different server

Our current hosting company is based in Canada so obviously their times are different to the UK.
Is there any way in PHP to display the Date/Time but get it from a different server if i specify the IP?
you can use date_default_timezone_set (see documentation) function to set your actual script time to whatever you need
example for Rome
date_default_timezone_set('Europe/Rome');
Using timezones would be a better idea.
$now = new DateTime(null, new DateTimeZone('America/Toronto'));
$now->setTimezone(new DateTimeZone('Europe/London'));
echo now->format('Y-m-d H:i:s);

PHP Datetime Error

I am having an issue with php date() function.
When I save the date in mysql datebase the hours shows 4 hours less than my current time.
My php code is below: $add_date = date("Y-m-d H:i:s");
It saves the time in database as
2011-08-03 07:51:26
But is should show
2011-08-03 13:22:26
Can anybody tell me how to fix it
Thanks
You have to set a valid time zone - it seems you don't have an appropriate time zone set up in php environment for your location.
Check out
http://www.php.net/manual/en/function.date-default-timezone-set.php
It should give you a clue how to set up the correct time zone in php.
#Mujahid If the printed time and the saved time are the same it probably means your server is not in the same timezone as you. With that said, you have to manually set the default timezone for your PHP script at the top of the file, or get the DateTime by defining your timezone explicitly. Here's the code:
$dateTime = new DateTime("now", new DateTimeZone('America/Los_Angeles'));
$add_date = $dateTime->format("Y-m-d H:i:s");
echo $add_date;
Here's a list of time zones that PHP supports, just find yours and replace America/Los_Angeles with it.
http://php.net/manual/en/timezones.php
Here's a good tutorial on PHP DateTime and DateTimeZone...
http://ditio.net/2008/06/03/php-datetime-and-datetimezone-tutorial/
Hope this helps. Good luck.
Make the time zone setting of the MySQL server and the web server the same.
this will add hosted server time not local time
$add_date = date("Y-m-d H:i:s");
use strtotime() to add hours and minutes
$newdate = date("Y-m-d H:i:s",strtotime('+ 5 hours 30 minutes'.$row['db_date']));

Categories