solidstan Posted February 20, 2014 Share Posted February 20, 2014 I have a working php file, which use lots of custom dates. I am try to simply format an echoed out put, with no success(very new to php). hope someone can help. $rotaDate = date('23/2/14');$driverListDate = date('5/1/14'); //Calculate the differance between 2 dates function dateDiffInWeeks($date1, $date2,$noOfLines){ $first = DateTime::createFromFormat('d/m/Y', $date1); $second = DateTime::createFromFormat('d/m/Y', $date2); return (((floor($first->diff($second)->days/7))/$noOfLines) - floor((floor($first->diff($second)->days/7))/$noOfLines))*$noOfLines; } function customData($con,$num,$driverListDate,$rotaDate,$noOfLines,$line){ if($line==getNewLine($con,$num,$driverListDate,$rotaDate,$noOfLines)) { //echo date("jS F Y" ); //echo date('jS F Y', $rotaDate); return $rotaDate; } } Im trying to echo my date as "jS F Y" format. This of course is not my full php file, but im sure its all thats relevant. Thanks in advance! Link to comment Share on other sites More sharing options...
justsomeguy Posted February 20, 2014 Share Posted February 20, 2014 If $rotaDate is a string then you need to convert it to a DateTime object like you do in your first function, and then you can use DateTime::format to output it: http://www.php.net/manual/en/datetime.format.php Link to comment Share on other sites More sharing options...
solidstan Posted February 20, 2014 Author Share Posted February 20, 2014 Im sorry. Im new very new to php. Are you asking me if $rotaDate is a string? Im not sure, that is why I showed it here:- $rotaDate = date('23/2/14'); If I change this format in any shape or form, the that second function(which I copyed and pasted from the net) Fails! Right now, my php page works perfect, except for the format that it outputs. Link to comment Share on other sites More sharing options...
solidstan Posted February 20, 2014 Author Share Posted February 20, 2014 echo date_format($rotaDate, 'g:ia on l jS F Y'); I copyied this from the page you showed me, but AGAIN.. It Fails! Warning: date_format() expects parameter 1 to be DateTime, string given in Link to comment Share on other sites More sharing options...
solidstan Posted February 20, 2014 Author Share Posted February 20, 2014 If anybody can go the extra mile, and give me a physical Example... Please do. If i knew how to do this.. I would'nt be asking! Link to comment Share on other sites More sharing options...
thescientist Posted February 20, 2014 Share Posted February 20, 2014 (edited) echo date_format($rotaDate, 'g:ia on l jS F Y'); I copyied this from the page you showed me, but AGAIN.. It Fails! right, so as JSG was trying to get you to see, $rotaDate is a string, not a date object. If $rotaDate is a string then you need to convert it to a DateTime object like you do in your first function, and then you can use DateTime::format to output it: http://www.php.net/manual/en/datetime.format.php like you already doing here $first = DateTime::createFromFormat('d/m/Y', $date1); Edited February 20, 2014 by thescientist Link to comment Share on other sites More sharing options...
solidstan Posted February 20, 2014 Author Share Posted February 20, 2014 function customData($con,$num,$driverListDate,$rotaDate,$noOfLines,$line){ if($line==getNewLine($con,$num,$driverListDate,$rotaDate,$noOfLines)) { //echo date("M d, Y" ); //echo date('M d, Y', $rotaDate); // return $rotaDate; $rotaDate = DateTime::createFromFormat('d/m/Y', $date1); echo $date1; echo $rotaDate; } } No, This does nothing! Link to comment Share on other sites More sharing options...
solidstan Posted February 20, 2014 Author Share Posted February 20, 2014 Aghhh, my head is spinning. I'm sure this should be easy.Can anybody please SHOW ME, what I'm looking for.I'm really not getting this.Looked at loads of web pages, tried loads of ways. Nothing seems to work. Link to comment Share on other sites More sharing options...
solidstan Posted February 21, 2014 Author Share Posted February 21, 2014 function customData($con,$num,$driverListDate,$rotaDate,$noOfLines,$line){ if($line==getNewLine($con,$num,$driverListDate,$rotaDate,$noOfLines)) { //echo date("M d, Y" ); //echo date('M d, Y', $rotaDate); $date = DateTime::createFromFormat('d/m/Y', $rotaDate); return $date; Catchable fatal error: Object of class DateTime could not be converted to string Think im gona have to give up with this stuff. Link to comment Share on other sites More sharing options...
thescientist Posted February 21, 2014 Share Posted February 21, 2014 (edited) So first thing is to know what you are working with. what do you get for output here if you echo? Do you know what you should get? $rotaDate = date('23/2/14');$driverListDate = date('5/1/14'); The manual for date say's that you provide a _format_ as the first parameter and optional timestamp. in return you get a date _string_ http://www.php.net/manual/en/function.date.php I would personally try and stick with just working with either the procedural style or the object oriented style of date. I would try and create your initial dates as DateTime objects as such and then you can output the format simply and in any number of ways you need. Just make sure your functions expect your dates as DateTime objects. $rotaDate = new DateTime(); //returns a DateTime objectecho 'Date => ' . $rotaDate->format("jS F Y"); http://www.php.net/manual/en/datetime.construct.php http://www.php.net/manual/en/ref.datetime.php Edited February 21, 2014 by thescientist Link to comment Share on other sites More sharing options...
solidstan Posted February 21, 2014 Author Share Posted February 21, 2014 Thanks, you make it sound easy. I was getting "23/2/14" which I assume was a string. I had tried so many different ways, I was convinced this was never going to work. I had tried the datetime() function, but could only get current date. Your Solution works perfectly. Thanks Millions. my code is procedural, the bit of Object O posted above was what I have copied from elsewhere. Thanks again, I now understand.. Solved! Link to comment Share on other sites More sharing options...
swafetywatto Posted February 24, 2014 Share Posted February 24, 2014 Glad I stumbled on this as I've had a simialr issue which I've now resolved :-) thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now