I have the following code
date("D j M g a", strtotime($start))." - " . date("ga", strtotime($finish));
`["Start"]=>
string(21) "/Date(1354323600000)/"
["End"]=>
string(21) "/Date(1354326300000)/"
}`
The above code outputs Thu 1 Jan 12 pm - 12pm" string(22) "Thu 1 Jan 12 pm - 12pm in the correct format that I want but how could I get the day date and time correct.
Pacific/Auckland is my timezone
The issue is that what you pass to the strtotime - /Date(1354323600000)/ string is not in a valid format for strtotime function.
And actually it's a regular unix timestamp in milliseconds.
So the obvious solution - is to clean up everything but numbers and divide it by 1000:
$unix_time_with_ms = preg_replace('~\D~', '', $date);
echo date('Y-m-d H:i:s', $unix_time_with_ms / 1000);
Prefer setting default timezone in your php.ini file with a line like this:
date.timezone = "Pacific/Auckland"
If for some reason you can't edit php.ini then on top of your PHP script you can set a timezone (effective for that script only):
date_default_timezone_set('Pacific/Auckland');
You can set time zone using
date_default_timezone_set('America/Los_Angeles');
More Link
if you want to set in the script try
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
date_default_timezone_set('America/Los_Angeles');
but if you want to use the same time zone than setting in php.ini will be better solution
date.timezone = "America/Los_Angeles"
Related
I'm using date function $now=date("Y-m-d H:i:s"); in php 5.6.30,
The browser output is string(19) "2017-04-21 02:54:54",That's abnormal.
php.ini set is date.timezone = PRC
Centos 7 system time:
[root#localhost sync]# date
Fri Apr 21 14:53:20 CST 2017
While I was installed the PHP 7.0.16 in the same system, php.ini has the same config, but the date output normal in date function(is 24 digit time).
Why happen this and how to let the date normal working.
This is a timezone issue.
In your php.ini, you have the timezone PRC (China) set, but your system time output gives the time in CST (Amerika).
In other words:
Your PHP code does actually give you the 24-hour format, but in a different timezone, where it is in fact 02:54:54
If you need to get the time in a different timezone (like UTC), you can set it like that:
date_default_timezone_set('UTC');
If you need a different local timezone, you can read about the possible values in the list of supported timezones in the PHP documentation
try this below code for date timezone
<?php
date_default_timezone_set('Asia/Shanghai');
echo date("Y-m-d H:i:s");
?>
I'm add the code to php.ini date.timezone = Asia/Shanghai,
and install chrony and start it to synchronized. It works.
I am running XAMPP on mac.
I have a very simple script:
<?php
echo time();
always returns time in the 1970.
example: 1475044574 (Edit: 1475137157)
I tried setting the timezone in php.ini and then calling date_default_timezone_get() and it returns the correct set value.
I tried adding SetEnv TZ MyTimezone to httpd.conf (at the bottom of the file) and it did not work.
I also tried setting the timezone in php with date_default_timezone_set() and though it sets successfully, time is still in 1970.
I tried the same script on MAMP and still the same problem.
Any suggestion is appreciated!
time function is returning proper unix timestamp you are doing mistake in converting time into proper format. 1475044574 means Wed, 28 Sep 2016 06:36:14 GMT which is correct.
You can convert time as you want. refer date formats.
check this sample code :
echo date('M j Y g:i A', 1475044574);
Bro, as per PHP manual php.net/manual/en/function.time.php time() function returns seconds not miliseconds. Please check timestamp your timestamp on this site epochconverter.com.
time() function returns current Unix timestamp. So, your 1475044574 is a timestamp.
Using date function you can see that this is a timestamp of (some variations with timezones, results may vary):
echo date('Y-m-d H:i:s', 1475044574);
// 2016-09-28 02:36:14
If you convert this timestamp to some string representation and get something like 00-00-1970 - you obviously do the conversion wrong.
My date is ahead 1 hour when I post to mySQL via php. I'm in Los Angeles. How can i make the time correct?
Here is what I currently have:
$date = date("m/d/y g:i A") ;
Use this to get the time zone PHP is using date_default_timezone_get();
If you're in Los Angeles you can set your time zone temporarily for only the current script
date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Also pass T in the date function:
echo date("D M j G:i:s T Y"); outputs Mon May 25 16:23:49 EDT 2009
see
date_default_timezone_set
like
date_default_timezone_set('America/Los_Angeles');
First check if servers time is correct ( on linux use date shell command). If yes, you just set the time and you're good.
If not you have to change it in php with setlocale() or date_default_timezone_set() function
It depends on your specific case, but I had saved the date as UTC / GMT time, so I had to use the gmdate function instead of date. E.g.
gmdate("H:i",$time)
I think this is a stupid question, but seems that I cannot find the answer.
I have this timestamp: 1295598602.
In my php script I have:
$date = date('Y-m-d', 1295598602);
$hour = date('H', 1295598602) . ':00';
This returns:
Date: 2011-01-21
Hour: 03:00
Now I went to an online conversion site to test this. I used this one.
But it seems that for this timestamp value it is
Fri, 21 Jan 2011 08:30:02 GMT
Now, which one is correct?
Use correct timezone:
>> date_default_timezone_get();
'UTC'
>> date('Y-m-d h:i:s',1295598602);
'2011-01-21 08:30:02'
>> date_default_timezone_set('CET');
true
>> date('Y-m-d h:i:s',1295598602);
'2011-01-21 09:30:02'
>> date_default_timezone_set('UTC');
true
>> date('Y-m-d h:i:s',1295598602);
'2011-01-21 08:30:02'
In GMT / UTC (they're almost but not quite exactly the same) that timestamp is indeed Fri, 21 Jan 2011 08:30:02 GMT.
If you're in a different timezone but always want GMT you'll need to use the gmdate() function instead of date().
Both are correct. In the code snippet PHP adjusts for timezone. Try date_default_timezone_set('UTC'); to get proper unadjusted values.
Another option is to set the default timezone for your script.
For example,
date_default_timezone_set('Europe/London');
$timestamp = '1295598602';
echo date('Y-m-d H:i:s', $timestamp);
would get you the same result as the online conversion tool is showing.
There are a number of timezone-related function in PHP that will allow you to modify which time zone is being shown.
You can check the PHP docs for a list of your options: http://www.php.net/manual/en/ref.datetime.php
According to date() function description,
timestamp is optional and defaults to
the value of time().
And according to time() function description, it returns GMT timestamp.
So, PHP does conversion to your time zone, while onlineconversion.com does not.
strtotime is being calculated from 1970-01-01 (UTC) and date is using my timezone i.e America/New_York
A simple example:
Where timestamp is 0:
var_dump(date('Y-m-d h:i:s', 0));
prints: 1969-12-31 07:00:00
Where it should return 0, it returns
var_dump(strtotime('1969-12-31 07:00:00'));
-43200
Now, obviously, I could add the timezone to the date i.e
strtotime($date . ' -5')
and it'd work.
But, I'm using the strtotime function everywhere.
I tried playing with the _$ENV['TZ'] and the date_timestamp_get();
But I can't sync them.
I also modified the date.timezone in php.ini to America/New_York and still nothing.
Finally, forcing the timezone using date_timestamp_set('UTC'), works fine and both dates are synced. But, I need the timezone from the server.
Please advise, I'm out of ideas
var_dump(date('Y-m-d H:i:s', 0));
h != H in date. It is 7pm, and you're trying to get the time from 1969-12-31 07:00:00am after.