Php date function - php

I can't see my location time with following php function:
echo date("y-m-d h:m:s A");
it's show 12-05-05 08:05:12 AM BUT it's 12-05-05 12:40:12 PM NOW
Can anyoe fix this issue..
Thanks.

Set your preferred timezone in php.ini:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Manila
You can see the list of supported timezones here.
Or you can go other way, use date_default_timezone_set().

Set setlocale function. Here this the description

Fix your server time, or use this:
http://php.net/manual/en/function.date-default-timezone-set.php

You are using m, but it's used to represent month, not minutes.
This is probably what you'd like to do:
echo date("y-m-d h:i:s A");
After that, you're either using the wrong timezone or the servers time is badly setup. If you have access to the server run date in a terminal.

Apart from Blake comment, i suggets try fixing your time zone, add +/- time you require
time , example
$timediff = 10;
$diff= $timediff+ ($timediff* 60 * 60)

Related

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

Creating a EDT (Eastern Daylight Time) - timestamp

I tried using the php function below to accomplish this, however, it returns a mounain time, timestamp. I was wondering if someone can help me understand how I change time zones, or if someone can help me correct this to EDT?
date("F j, Y, g:i a");
You can either set it locally in the script itself, by using the date_default_timezone_set() function:
http://php.net/manual/en/function.date-default-timezone-set.php
Or you can change it in your php.ini file, if you look for the line:
date.timezone
Edit: Here is a list of supported timezones, if you're not sure what yours should be called:
http://php.net/manual/en/timezones.php

does php's date_default_timezone_set adjust to daylight saving?

Does php's date_default_timezone_set adjust to daylight saving?
I have this code, and wonder if it will always result in the correct Stockholm time?
date_default_timezone_set('Europe/Stockholm');
$timestamp = date("Y-m-d H:i:s");
PHP doesn't handle DST automatically. You have to check
if (date('I', time()) == 1) ... the time is in DST mode ("0" = not)
Then you should adust time accordingly.
(Note: 'I' in capital. I have just checked it and it works.)
Yes this should always result in the right time.
As long as your timezone is listed in the following link, timestamp should be relative to the correct timezone.
http://www.php.net/manual/en/timezones.php

How do I get a date in UK local time using PHP?

I'm using the unix timestamp to show when a message was posted in my project but when I came to displaying the exact time of the post I realized it was about 12 hours behind.
I'm in the UK and my server is in the US (might be the problem).
Is there a simple way of converting the unix timestamp into a readable British time?
$timestamp = time();
print date("F jS, Y", strtotime($timestamp));
Any help would be great,
Thanks!
At the top of your script, write:
date_default_timezone_set('Europe/London');
Or if your PHP is >= 5.2.0:
date_timezone_set('Europe/London');
Just call date_timezone_set with the appropriate parameter for the UK at the start of your script when displaying the dates (not when recording them; I 'm not sure, but it might result in the "wrong" timestamps being recorded).
Edit: The timezone you want is 'Europe/London'.
try date-default-timezone-set.
date_default_timezone_set('Europe/London');
Use date_default_timezone_set('Europe/London'); to set the time zone to London's time. Not sure if it works with summer/winter time.
the simplies way is to substruct gmt offset. e.g:
echo(date('Y-m-d h:i'), $myvalue - 60 * 60 * $nhours));
where $nhours - time defference in hours.
This one worked for me
date_default_timezone_set('Europe/London');

Php clock 2 hours back

So the clock is 18:37 right now in sweden but it prints out 16:37 why is that?
$timestamp = time();
date('M d, H:i', $timestamp)
What can be wrong?
Your date.timezone setting in your php.ini file is incorrect. Make sure that it is set to the proper value for your timezone:
date.timezone = Europe/Stockholm
If you do not have access to the php.ini file, you can use date_default_timezone_set() to set it during runtime:
date_default_timezone_set('Europe/Stockholm');
For a list of supported timezones, refer to the PHP Documentation.
If it still doesn't work, make sure your server is set to the proper timezone. If you've set the time manually and the timezone is incorrect (but since the time has been corrected manually it still shows the proper time), PHP has no way to get the UTC time properly and therefore returns the incorrect time.
It is possible that your server is located in a time that is 2 hour back from you.
You can use this page of the documentation to fix the timezone issue.
Try a line like this:
date_default_timezone_set('America/New_York');
Except, y'know, for Sweden.

Categories