Hi all:

It appears that sessions are not available for date time objects pre php 5.3.

Warning: date_format() [function.date-format]: The DateTime object has not been correctly initialized by its constructor

Is there a standard work around for this or do I need to pass a querystring (hope not!):mad:?

    I'm assuming you do not actually try to store a datetime object as session data. Have you made certain that you user serialize when you store session data and unserialize when you retrieve session data?

      johanafm;10978077 wrote:

      I'm assuming you do not actually try to store a datetime object as session data. Have you made certain that you user serialize when you store session data and unserialize when you retrieve session data?

      I didn't even know about serialize/unserialize! But I added those functions and now get:

      Fatal error: Can't use function return value in write context in

        No luck on this at all.

        When I place the following on the page where I am initializing the session ti works fine:

        echo date_format($_SESSION["eventDate"],"m/d/Y");
        

        When I place it on a separate page I get the following:

        Warning: date_format() [function.date-format]: The DateTime object has not been correctly initialized by its constructor in C:\Web\MySite\adm\adm_moSubmit_.php on line 87

        Both the initial page and display page both have session_start about the HTML.

        Any help appreciated!

          Why is this code not working?

          echo $stuff;
          

          Well, pretty hard to know. Maybe $stuff hasn't been defined. Maybe I have a parse error on the line before it. Maybe... I should post an error message with my code, and maybe I should post the code I need help with instead of one single line and maybe I also should explain what isn't working. All of these really are needed to get help.

          Fatal error: Can't use function return value in write context in

          Maybe you should post some code with your error message

          Where's your serialize?
          Where's your unserialize?

            Here is more info:

            This is taken from the page that is setting the session:

            
            $_SESSION["l_id"]			= $_POST["l_id"];
            $paramsListing = array($_SESSION["l_id"]);
            
            $sqlListing = "SELECT Player, Promoter, EventDate FROM Events WHERE l_id = ?";
            
            $stmtListing = sqlsrv_query($conn, $sqlListing, $paramsListing);
            $resultListing = sqlsrv_fetch_array($stmtListing);
            
            $_SESSION["player"]		= $resultListing["Player"];	$_SESSION["promoter"]		= $resultListing["Promoter"];				
            $_SESSION["eventDate"]	= $resultListing["EventDate"];
            

            On the display page is:

            echo date_format($_SESSION["eventDate"],"m/d/Y"); 
            

            As for serialize/unserialize I am not quite sure why they are needed. I am using procedural coding. $SESSION["player"] & $SESSION["promoter"] are both working fine without serializing.

              Because player is a string, while datetime isn't. Serializing a datetime object turns it into a string, which can be turned into a datetime object using unserialize.

                johanafm;10978296 wrote:

                Because player is a string, while datetime isn't. Serializing a datetime object turns it into a string, which can be turned into a datetime object using unserialize.

                Still not getting this one to work. This is on the display page:

                $rEventDate 	=		serialize($_SESSION["eventDate"]);
                echo $rEventDate;
                

                The display is "O:8:"DateTime":0:{}"

                The session is good because when I echo the session on the page where I am setting the session , the proper date appears.

                  # SESSION: initializing, updating etc
                  if (!isset($_SESSION['datetime']) && isset($datetime))
                  {
                  	$_SESSION['datetime'] = serialize($datetime);
                  }
                  
                  # on every page that needs the datetime
                  if (isset($_SESSION['datetime']))
                  {
                  	$datetime = unserialize($_SESSION['datetime']);
                  }
                  echo $datetime->format('Y-m-d');
                  

                    Thanks you again! Now it seems so easy!

                      Write a Reply...