Jump to content

need help creating "MY" date object.


Go to solution Solved by kicken,

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.

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

  • Solution

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);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.