Jump to content

Create a Date and Time from two fields


bravo14

Recommended Posts

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 comment
https://forums.phpfreaks.com/topic/255276-create-a-date-and-time-from-two-fields/
Share on other sites

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

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);

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);

?>

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);

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;

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.