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

Link to comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.