Darren_Stanley Posted March 15, 2014 Share Posted March 15, 2014 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 More sharing options...
Darren_Stanley Posted March 15, 2014 Author Share Posted March 15, 2014 OK, the Highlight didn't work in code. But the line in question, if you haven't already guessed. $rotaDate= new DateTime(); //returns a DateTime object Link to comment https://forums.phpfreaks.com/topic/287002-need-help-creating-my-date-object/#findComment-1472743 Share on other sites More sharing options...
kicken Posted March 16, 2014 Share Posted March 16, 2014 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); Link to comment https://forums.phpfreaks.com/topic/287002-need-help-creating-my-date-object/#findComment-1472745 Share on other sites More sharing options...
Darren_Stanley Posted March 16, 2014 Author Share Posted March 16, 2014 Kicken, Perfectly explained. My Problem solved.. Thanks Millions. Link to comment https://forums.phpfreaks.com/topic/287002-need-help-creating-my-date-object/#findComment-1472767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.