bravo14 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. Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/ Share on other sites More sharing options...
dzelenika 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'); Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308833 Share on other sites More sharing options...
bravo14 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 Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308836 Share on other sites More sharing options...
dzelenika Posted January 18, 2012 Share Posted January 18, 2012 $end_date_time=DateTime::createFromFormat( 'Y-m-d H:i:s', $end_date . $end_time); Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308841 Share on other sites More sharing options...
bravo14 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 Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308843 Share on other sites More sharing options...
dzelenika Posted January 18, 2012 Share Posted January 18, 2012 could you send me an output from: echo $end_date . $end_time; Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308870 Share on other sites More sharing options...
bravo14 Posted January 18, 2012 Author Share Posted January 18, 2012 As requested the output of that is 2012-01-2212:00:00 Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308877 Share on other sites More sharing options...
dzelenika 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); Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308879 Share on other sites More sharing options...
bravo14 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); Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308881 Share on other sites More sharing options...
dzelenika 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308885 Share on other sites More sharing options...
bravo14 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); Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308892 Share on other sites More sharing options...
dzelenika Posted January 18, 2012 Share Posted January 18, 2012 in which line is error? 3rd or 5th? Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308904 Share on other sites More sharing options...
bravo14 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; Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308908 Share on other sites More sharing options...
dzelenika 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'); Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308926 Share on other sites More sharing options...
bravo14 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? Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308931 Share on other sites More sharing options...
bravo14 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. Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1308939 Share on other sites More sharing options...
litebearer Posted January 19, 2012 Share Posted January 19, 2012 what is the type for your db field? Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1309080 Share on other sites More sharing options...
Pikachu2000 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) ) Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1309126 Share on other sites More sharing options...
bravo14 Posted January 19, 2012 Author Share Posted January 19, 2012 The field type if datetime Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1309158 Share on other sites More sharing options...
bravo14 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. Quote Link to comment https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/#findComment-1309162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.