bravo14 0 Posted January 18, 2012 Share Posted January 18, 2012 Hi guys I hav a form where a user selects an end time and the date will be 4 days from today. I have been able to get the date to populate, but I am trying to join the two fields together but with no luck. I have the end date caluclated using the code below and this works $end_date=date("Y-m-d",$dateplus4); The end time is selected from a menu where the user selects either 12 or 17 (12:00:00 or 17:00:00) I have tried $end_date_time=$end_date." ".$end_time; I have also tried strtotime and mktime, but obviuosly not doing it properly. Any help would be great. Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 Try with $date = DateTime::createFromFormat( 'Y-m-d H:i:s', '2009-02-15 15:16:17'); Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 I have now changed it to below $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date $end_time); I am getting the following error Parse error: syntax error, unexpected T_VARIABLE in /home/sites/trade-bidz.co.uk/public_html/add-vehicle.php on line 40 Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . $end_time); Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 Tried that and getting an error of Object of class DateTime could not be converted to string Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 could you send me an output from: echo $end_date . $end_time; Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 As requested the output of that is 2012-01-2212:00:00 Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 as you can see there is a space missing try with: $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . " " . $end_time); Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 I am still getting the same error, if it helps I have copied how I get ther end_date and end_time $end_date=date('Y-m-d',strtotime($date .'+4 days')); $end_time=$_POST['end_time'].':00:00'; $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . " " . $end_time); Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 You haven't set timezone. Try with this: <?php date_default_timezone_set('America/Los_Angeles'); // <- set timezone first $end_date=date('Y-m-d',strtotime($date .'+4 days')); $end_time=$_POST['end_time'].':00:00'; $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . " " . $end_time); ?> Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 I am still getting Catchable fatal error: Object of class DateTime could not be converted to string the php is date_default_timezone_set('Europe/London'); // <- set timezone first $date=date('Y-m-d'); $end_date=date('Y-m-d',strtotime($date .'+4 days')); $end_time=$_POST['end_time'].':00:00'; $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . " " . $end_time); Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 in which line is error? 3rd or 5th? Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 I am trying to echo the $end_date_time line and the error is on the echo line, so my thoughts were that the $end_date_time variable is causing the error date_default_timezone_set('Europe/London'); // <- set timezone first $date=date('Y-m-d'); $end_date=date('Y-m-d',strtotime($date .'+4 days')); $end_time=$_POST['end_time'].':00:00'; $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . " " . $end_time); $min_bid=$_POST['reserve']; echo "End Time is ".$end_time; echo "<br/>Today is ".$date; echo "<br/>End Date is ".$end_date; echo "<br/>End Date and Time is ".$end_date_time; Link to post Share on other sites
dzelenika 0 Posted January 18, 2012 Share Posted January 18, 2012 you can't echo $end_date_time because it is not string but you can do echo $end_date_time->format('Y-m-d H:i:s'); Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 Will I be able to add the $end_date_time to a database as it is? Link to post Share on other sites
bravo14 0 Posted January 18, 2012 Author Share Posted January 18, 2012 I have tried adding the field to a database and I am getting the same error message but on a different line in the code. Link to post Share on other sites
litebearer 5 Posted January 19, 2012 Share Posted January 19, 2012 what is the type for your db field? Link to post Share on other sites
Pikachu2000 121 Posted January 19, 2012 Share Posted January 19, 2012 You don't really even need to use comparatively slow php functions to calculate the dates to insert into your DB, as long as the fields are DATE data type. Example: INSERT INTO table ( start_field, end_field ) VALUES ( CURDATE(), DATE_ADD(CURDATE(), INTERVAL 4 DAY) ) Link to post Share on other sites
bravo14 0 Posted January 19, 2012 Author Share Posted January 19, 2012 The field type if datetime Link to post Share on other sites
bravo14 0 Posted January 19, 2012 Author Share Posted January 19, 2012 You don't really even need to use comparatively slow php functions to calculate the dates to insert into your DB, as long as the fields are DATE data type. Example: INSERT INTO table ( start_field, end_field ) VALUES ( CURDATE(), DATE_ADD(CURDATE(), INTERVAL 4 DAY) ) If I was to do this how would I get the end time that is selected from a menu of either 12:00:00 or 17:00:00 into the db. Link to post Share on other sites
Recommended Posts
Archived
This topic is now archived and is closed to further replies.