Merge Date and Time elements

keianrad

Member
I have a from for booking meetings. There is a date element for picking meeting date and a Time element for picking Time meeting. I need to save both picked date and Time in another element named "Start date" and use them as meeting date in calendar and ...

How can I merge them and save them in the start date element?

for Example:

Date: 08/20/2017
Time: 10:30

Start Date: 08/20/2017 10:30:00

Thank you for your help.
 
You'd probably need to use a form submission script.

Code:
// get start date and create a PHP date object form it
$myDate = $formModel->getElementData('yourtable___meeting_date', true);
$myDate = new DateTime($myDate);

// get the time, which will be an array with 3 entries, 0 is hours, 1 is minutes, 2 is seconds
$myTime = $formModel->getElementData('yourtable___meeting_time', true);

// set the time on the date object
$myDate->setTime($myTime[0],$myTime[1]);

// make it a formatted string ...
$myDate = $myDate->format('Y-m-d H:i:s');

// set the start date
$formModel->setFormData('yourtable___start_date', $myDate, true);

I would suggest including this into the class I created for you.

-- hugh
 
Thank you Hugh. I tried the code listed below in a PHP form plugin with both End of Submission.. (OnAferProcess) and Start of submission.. (OnAferProcess). but it doesn't work.

Code:
// get start date and create a PHP date object form it
$myDate = $formModel->getElementData('app_events___date', true);
$myDate = new DateTime($myDate);

// get the time, which will be an array with 3 entries, 0 is hours, 1 is minutes, 2 is seconds
$myTime = $formModel->getElementData('app_events___time', true);

// set the time on the date object
$myDate->setTime($myTime[0],$myTime[1]);

// make it a formatted string ...
$myDate = $myDate->format('Y-m-d H:i:s');

// set the start date
$formModel->setFormData('app_events___start_date', $myDate, true);

when I try with Start of submission.. (OnAferProcess) I get this error:

DateTime::__construct() expects parameter 1 to be string, array given
 
Last edited:
I think that the data you get from the model is formatted as an array; today would be with one element for each value $mydate[0]=2017, $mydate[1]=08, mydate=[2]=21.
DateTime() expects a string like '2017-08-21'
I think you'll have to do something like
$myDate = impode('-', $forModel->getElementDate('app_events___date', true);

This should convert it to a string '2017-08-21'.
BUT - you'd better check my logic with Jdump!

Bob
 
Thank you bob. I tried to use this code on Start Submission but it shows "Call to a member function getElementData() on null" error after sumbiting.

Code:
// get start date and create a PHP date object form it
//$myDate = $formModel->getElementData('app_events___date', true);
$myDate = implode('-', $forModel->getElementDate('app_events___date', true));
$myDate = new DateTime($myDate);

// get the time, which will be an array with 3 entries, 0 is hours, 1 is minutes, 2 is seconds
//$myTime = $formModel->getElementData('app_events___time', true);
$myDate = implode('-', $forModel->getElementDate('app_events___time', true));

// set the time on the date object
$myDate->setTime($myTime[0],$myTime[1]);

// make it a formatted string ...
$myDate = $myDate->format('Y-m-d H:i:s');

// set the start date
$formModel->setFormData('app_events___start_date', $myDate, true);
 
Hmm.
To really understand what is going on, you will need to dump the variables and see what is being manipulated.
I use the J!Dump extension. (https://extensions.joomla.org/extensions/extension/miscellaneous/development/j-dump/)
Install this extension on your site then, in your code, you can put dump statements and see what the variables are. J!dump will open a new window you can look at.
NOTE - sometimes that new window is 'behind' others so you may not spot it.
For instance - this code will show you the contents of $myDate after each step.
You may want to use different variable names for testing. I think it will work to re-assign $myDate, but eh...

// get start date and create a PHP date object form it
$myDate = $formModel->getElementData('app_events___date', true);
dump($myDate,'Mydate from FormModel');
$myDate = implode('-', $myDate);
dump($myDate, 'Mydate after implode');
$myDate = new DateTime($myDate);
dump($myDate,'Mydate after new DateTime');

This will show you precisely what is being returned at each step.
Careful of quotes (') and semicolons (;)

If you don't want to use J!Dump, you can use var_dump, but it is not as 'nice' in that it is not in a separate window and you may loose it.
 
Okay- so I decided to dig into this and got it working for you.

BTW
Lesson one - don't question Hugh. You do not need to do the implode stuff I mentioned.
Lesson two - none of us is perfect. I changed the last line of Hugh's code from
setFormData
to
updateFormData

I created a test form and did pretty much what you wanted I think. There are a number of inter-related steps.

1) Run the code both OnBeforeProcess. If you do it OnAfterProcess the update does not work because the data is already written.
2) In your element "start_date" you must add a format for the list and form. The default format is Y-m-d so it will not show the time. I set "Y-m-d H:i" which gives you the year-month-date Hour:minute (hour is in 24 hr format). Put this in the Details, Date Format dialog under List and Form Format for the start_time element you are setting.

You'll see I have dump commands in there - they are for J!Dump. If you do not have J!Dump, it will not work.
Note - my test list is called "test_list"

---
// get start date and create a PHP date object form it
$myDate = $formModel->getElementData('test_list___test_date', true);
dump($myDate,'Mydate from FormModel');

$myDate = new DateTime($myDate);
dump($myDate,'Mydate after new DateTime');

// get the time, which will be an array with 3 entries, 0 is hours, 1 is minutes, 2 is seconds
$myTime = $formModel->getElementData('test_list___test_time', true);
dump($myTime,'MyTime from FormMOdel');

// set the time on the date object
$myDate->setTime($myTime[0],$myTime[1],$myTime[2]);
dump($myDate,'MyDate after Set Time');

// make it a formatted string ...
$myDate = $myDate->format('Y-m-d H:i:s');
dump($myDate,'Mydate after format');
// set the start date
// THis is the line I changed.
$formModel->updateFormData('test_list___start_date', $myDate, true);

-----
Here is the output from J!Dump for each of the above. (I did not expand "Methods" as it wasn't relevant.)
I entered today (8/22/2017) and a time of 3:06:10 seconds


If you do this, the record looks like this.
test date test time start_date
2017-08-22 03:06:10 2017-08-22 03:06

Notice, I did not save the seconds.
-Bob
 
Thank you for taking time on my issue, It is working. big like. :)

Right know, I need to calculate 30 minutes after that time and save it to another date filed.

Thank you for your help.
 
You can use the add method to add 30 minutes to the date-time
Save the formated myDate in a separate variable so you can use the add function on the date. If you format it then try to add, it does not work
Then add 30 minutes. The syntax for DateInterval is very funky.
P means Period
T means Time element
30M means thirty minutes
P1D would be add 1 day.
PT1H would add 1 hour
I created a new element called 'new time' which has the 30 minutes added in.

Starting at (with no dumps) I've marked the changed variables in red.
// make it a formatted string ...
$myDateFormat = $myDate->format('Y-m-d H:i:s');
$formModel->updateFormData('test_list___start_date', $myDateFormat, true);

$myDate->add(new DateInterval('PT30M'));
$myDateFormat = $myDate->format('Y-m-d H:i:s');
$formModel->updateFormData('test_list___new_time',$myDateFormat,true);

-Bob
 
Bob, thank you for all that! I was off chasing the eclipse for a couple of days.

NOTE - I have a suspicion that after a failed validation, the date input may be in a different format, but I'll need to test that. I seem to recall some circumstances in which instead of a date string, it's an array of 'date' and 'time'.

-- hugh
 
Thank you bob this is the final code for both first section and the second one, but it shows an error:

Call to a member function format() on string

Code:
// get start date and create a PHP date object form it
$myDate = $formModel->getElementData('app_events___event_date', true);
//dump($myDate,'Mydate from FormModel');

$myDate = new DateTime($myDate);
//dump($myDate,'Mydate after new DateTime');

// get the time, which will be an array with 3 entries, 0 is hours, 1 is minutes, 2 is seconds
$myTime = $formModel->getElementData('app_events___time', true);
//dump($myTime,'MyTime from FormMOdel');

// set the time on the date object
$myDate->setTime($myTime[0],$myTime[1],$myTime[2]);
//dump($myDate,'MyDate after Set Time');

// make it a formatted string ...
$myDate = $myDate->format('Y-m-d H:i:s');
//dump($myDate,'Mydate after format');
// set the start date
// THis is the line I changed.
$formModel->updateFormData('app_events___date', $myDate, true);

// make it a formatted string ...
$myDateFormat = $myDate->format('Y-m-d H:i:s');
$formModel->updateFormData('app_events___date', $myDateFormat, true);

$myDate->add(new DateInterval('PT30M'));
$myDateFormat = $myDate->format('Y-m-d H:i:s');
$formModel->updateFormData('app_events___end',$myDateFormat,true);
 
The error is because the first time format() is called, it's being assigned back to $myDate ...

Code:
$myDate = $myDate->format('Y-m-d H:i:s');

... so $myDate is now a formatted string, not a date object. Which is OK in the code I gave in the first post, as it's not going to be used again. But if you want to use $myDate as an object again, you'll need to format it to a new variable, to keep $myDate hanging around as a DateTime object ...

Code:
// make it a formatted string ...
$myDateFormat = $myDate->format('Y-m-d H:i:s');
//dump($myDateFormat,'Mydate after format');
// set the start date
// THis is the line I changed.
$formModel->updateFormData('app_events___date', $myDateFormat, true);

-- hugh
 
Eclipse was cool. I'm jealous. I was in about 93% area and enjoyed it, but would liked to see the total.

Had fun with the date time and I may use this on the aviation site so it was worth effort.

I did run until the object vs formated issue but thought the code I ended up with worked. That's why I changed the variable names in the format. I'll look at my test code tomorrow when I'm not on a phone.
 
You changed the second use of format(), but not the first.

I think my final mileage for the weekend was 1,550 (although 300 of that was because I left my laptop behind and had to go back and get it). But worth every mile.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top