Jump to content

need help creating "MY" date object.


Darren_Stanley

Recommended Posts

HI, thanks for reading.

Im am VERY NEW at PHP and coding, please don't get confused with the simplicity of my request.(maybe its just a syntax issue), but im not sure.

 

 

First, I have set a variable:

$rotaDate = date('5/1/14');

Then I have made a function.

//This function determines and inserts the custom column data.
  function customData($line){

    if($line==getNewLine()) {
     $rotaDate= new DateTime(); //returns a DateTime object
    return $rotaDate->format("jS F Y");
    } else if($line>getNewLine()){
      $weeks=($line-getNewLine());
      $modifier=$weeks.' weeks';
        $rotaDate = new DateTime(); //returns a DateTime object
             $rotaDate->modify($modifier);
     return $rotaDate ->format("jS F Y");
    }
  }

The line highlighted in RED, sets $rotaDate to Todays date as an object, but i don't want that.

I need it to be an object date of MY $rotaDate = date('5/1/14') VARIABLE.

 

 

The above code works, but outputs TODAYS DATE(as you would expect).

any modification I make to this code creates an "not a date object" error.

 

I think what I am trying to do is make my VARIABLE($rotaDate) an OBJECT.

 

 

hope you can understand what Im trying to do.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/287002-need-help-creating-my-date-object/
Share on other sites

First off, this:

$rotaDate = date('5/1/14');
Is wrong. The date function accepts a format string (ie, 'm/d/Y') and returns a date in that format. Since your string doesn't have any format codes you get it back unchanged so it "works" but it's essentially the same as just doing:

$rotaDate = '5/1/14';
Which is what you should be doing to just assign a date string to a variable.

 

As for the line in your function, if you just want the static '5/1/14' date, then set that as the argument in the constructor for DateTime:

$rotaDate = new DateTime('2014-5-1'); 
Otherwise, pass in your $rotaDate variable as a second argument to use as a default. Eg

function customData($line, $defaultDate){
   if($line==getNewLine()){
     return $defaultDate->format("jS F Y");
   }
   ...
}

$rotaDate= new DateTime('2014-5-1'; //Or any other dynamically determined date
customData($line, $rotaDate);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.