gmmktime(): You should be using the time() function instead - php

What is the reason of the following error? How to solve the issue?
gmmktime(): You should be using the time() function instead
The issue in the line number 90:
89 date_default_timezone_set("GMT");
90 $time = gmmktime();

gmmktime() internally uses mktime() , which throws an E_STRICT notice when called with no arguments, so use the time() function instead.

This is a comment from php.net
gmmktime() should ONLY be used to create a timestamp when specifying a
specific GMT date and time.
If you want to make a valid time stamp for the current date & time,
use mktime() instead.
UNIX timestamps, by definition, store the GMT time relative to the
UNIX epoch.
gmmktime() (without any parameters specified) will effectively use the
computer's LOCAL time values just the same as if they were explicit
parameters, and the resulting time stamp will be incorrect. (The
resulting timestamp will actually be offset in the OPPOSITE direction
of the local timezone offset from GMT!)
So it requires input. You should consider this function as a conversion mechanism rather than a source of formatted data.

I suspect that your host may have updated php.
If you ask your host provider to edit your php.ini file, to disable logging of STRICT warnings in, php.ini, you may be sorted.
Also according to php.net
gmmktime() internally uses mktime() so only times valid in derived local time can be used.
Also check the change log there, it says
As of PHP 5.1.0, the is_dst parameter became deprecated. As a result, the new timezone handling features should be used instead.

The issue is that gmmktime() and mktime() require input, i.e. they take year, month, day, etc and give a timestamp. If you just want the current timestamp use the time() function.

This works fine for this purpose: gmdate( 'U' )

Related

Ignore BST with php strtotime() function

Is there a way for me to tell the strtotime() function not to change the time I give it into BST? i.e. if do
date('g.ia', strtotime("2014-06-25T19:30"))
I want to get 7:30pm, just as if I entered
date('g.ia', strtotime("2014-06-25T19:30"))
(The first one currently returns 6:30pm)
I'm aware I could just write a manual check for the day/month and add an hour if necessary, or just parse the time myself from the string, but both solutions sound a bit messy (I'll have to do this in quite a few places).
Sorry if there's something obvious I'm missing, pretty new to php
Function date() will format time based on your timezone setting. Said that, your example doesn't make sense since strtotime() will use current timezone setting to convert input to unix timestamp, and then function date() will use again that timezone setting to format timestamp back. You must be changing timezone setting between strtotime() and date() function calls, like this demo.
You can simply use DateTime extension, where you implicitly tell in what timezone is your time:
$dt = new DateTime('2014-06-25T19:30', new DateTimezone('Europe/London'));
echo $dt->format('g.ia');

how to change the default time zone in just one of my php file not config file? [duplicate]

This question already has an answer here:
Changing timezone in PHP
(1 answer)
Closed 9 years ago.
I have to change the time zone for one of my services; so just one file not in config for all php files! How can I do that! in that file I'm using filectime but apparently it is using another time zone!
Your help is appreciated.
If you need more clarification, please let me know
ADDED:
Sorry to say that but still after using date_default_timezone_set fro my city, my problem is not solved! I have a file the creation of which is : Nov 07 2013 7:36:50 but after using your time zone function for my city and following code:
$createTime = filectime($dirPath . '/' . $file);
the $creattime still it is : 2013-11-08T00:36:50Z
So it seems that time zone doe not have any effect on filectime! apart from adding hours (the difference hours) to the result of my filectime to adjust the difference, is there any other better ways?
Thanks
You might find php's date_default_timezone_set() helpful.
date_default_timezone_set() sets the default timezone used by all
date/time functions.
For example:
date_default_timezone_set('America/Los_Angeles');
Here is a list of supported timezones.
Edit:
I overlooked the fact that you're using filectime(). That function returns a unix timestamp, which does not contain any timezone data. So date_default_timezone_set() will not affect those results.
Instead, you can "translate" a date object from the server's timezone to a different timezone in PHP like this:
// for testing purposes, get the timestamp of __FILE__
// replace __FILE__ with the actual file you want to test
$time_created=filectime(__FILE__);
// build date object from original timestamp
$date=new DateTime(date('r',$time_created));
// translate the date object to a new timezone
date_timezone_set($date, timezone_open('America/Los_Angeles'));
// output the original and translated timestamps
echo"<p>Original Timestamp: ".date('r',$time_created)."</p>";
echo"<p>Los Angeles Timestamp: ".date_format($date,'r')."</p>";
Here's a working example.

Php Date Function

I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()

PHP date time to Unix time Stamp

How can I convert string "M-d-Y::H:i:s" to unix timestamp in php. I just realized manipulating date and time must be the most time consuming entity in any programming language.
Trivial Question: Why do you think there isn't any universal date time format. Why are there so many variations of same data? AArrhh.
The OOP way (requires PHP >= 5.3.0):
$dt = DateTime::createFromFormat("M-d-Y::H:i:s", $input);
$ts = $dt->getTimestamp();
You should of course check the return value of createFromFormat (it's false if an error occurs) but you should also definitely check DateTime::getLastErrors(); otherwise you might be surprised if e.g. your input has a day of "Jan 32". See my answer here for more info.
i have this issue service unavailable (with message) ResponseText: Error: Call to a member function getTimestamp() on boolean How to fixed this issue.

how to get the correct timestamp with the is_dst parameter in PHP?

We are creating a unix timestamp with the mktime method giving following attributes:
print_r(mktime(0,0,0,3,1,2009));
print_r(mktime(null,null,null,3,1,2009) / 60 / 60 / 24) . "days");`
this creates a result of
1235862000
14303.958333333 days
this should be 14304 days. The problem in this case is the winter hour.
You can use the is_dst parameter, and when we use 0 (default -1) this sometimes works correctly giving 14304 days as a result.
But sometimes this gives following problem:
Strict Standards: mktime() [function.mktime]:The is_dst parameter is deprecated
i have no idea what it means and what i can do about it. Any idea someone? because the winter hour is causing terrible head ache....
Use gmmktime.
date("I", time());
Will give you 1 if its daylight savings and 0 if its not.
From the php manual page:
"Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT message if using the system settings or the TZ environment variable. See also date_default_timezone_set()"
that probably means you should use date_default_timezone_set() previously and skip the is_dst parameter

Categories