PHP: DateTime '-1 day' -


I have to get a date date in a certain time zone. I tried this kind of thing, but it is not working: date = new date time (new, new date time zone ('Pacific / Wake')); $ Column = $ date- & gt; Modify ('-1 day'); $ Yesterday = $ tomorrow- & gt; Format ('Y-M-D');

I still get the date of today.

According to this problem, completely using php, depending on the version . In this case, method zodiacing (which is what you are trying to do) is available only on previously connected docs, only PHP version 5.3.0 and above.

The mind in it, tells you why your code does not work, and works like @Derrick. If you ever upgrade your PHP version, or your host can upgrade it , You might possibly deduct those three lines in two: date = new date time (zero, new datetime zone ('Pacific / wake')); $ Date = $ date- & gt; Modify ('-1 day') - & gt; Format ('Y-M-D');

No improvement, I know, but the reason is that why it failed to do the job.

There are two ways below which I know; A class is created .. which looks much for me until it is nothing different ... second is the composition of a ceremony. Both put some extra lines in something that take less space in one place.

  class DT {Private $ now; // can be an empty private $ timezone; // datetimejone object public function __conversion ($ tz_str, $ now = null) {$ this- & gt; Now = $ now; $ This- & gt; Timezone = new datetime zone ($ tz_str) ;; } Public function mod ($ mod, $ format) {$ dt = new date time ($ this-> now, $ this- & gt; timezone); $ Dt-> Modified ($ Modern); Return $ dt-> Format ($ format); }} Function mod_formatted_zone ($ tz_str, $ mod, $ format, $ now = null) {$ timezone = new datetime timezone ($ tz_str); $ Dt = new date time ($ now, $ timezone); $ Dt-> Modified ($ Modern); Return $ dt-> Format ($ format); }   

Either use is simple; In case of class, this will be something like this.

  $ dt = new DT ('Pacific / Wake'); Echo $ dt-> Mod ('- 1 day', 'Y-M-D');   

In the case of the function, it will simply ..

  echo mod_formatted_zone ('Pacific / Wake', '1 day', 'Ym-D ');    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -