PHP dates question - need to parse and stitch together form values to create date/time object

I'm developing a PHP-based app that uses date and time pickers.

The calendar picker that I'm using stores both the start & end dates in a single form field named "auction_dates", and stores them in the following fomat (ie: including the arrow separator):

2009-05-20 -> 2009-05-28

The time pickers that I'm using store the time values in 2 separate form fields named "time1" and "time2", and stores the times in the following 24-hour format:

14:15

Using PHP, --- I need to somehow parse the "auction_dates" field value (into 2 different dates -- startdate & endate), and then stitch things together in such a way that I end up with 2 different date/time objects (starttime & endtime) that are in the following format:

2009-05-27 10:07:00

How should I go about this?  

Thanks,
- Yvan
 

egoselfaxisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hube02Commented:
Give this a try, let me know if you have any questions.
<?php
  
  $dates = '2009-05-20 -> 2009-05-28';
  $time1 = '14:15';
  $time2 = '14:15';
  
  $regex = '/(\d{4}-\d{2}-\d{2}).*?(\d{4}-\d{2}-\d{2})/';
  
  preg_match($regex, $dates, $matches);
  
  $starttime = $matches[1].' '.$time1.':00';
  $endtime = $matches[2].' '.$time2.':00';
  
  echo 'Start Time: '.$starttime.'<br>End Time: '.$endtime;
  
?>

Open in new window

0
Hube02Commented:
Alternately, since you know the exact format of the dates field, you could use simple string functions:


<?php
  
  $dates = '2009-05-20 -> 2009-05-28';
  $time1 = '14:15';
  $time2 = '14:15';
  
  $starttime = substr($dates, 0, 10).' '.$time1.':00';
  $endtime = substr($dates, 14, 10).' '.$time2.':00';
  
  echo 'Start Time: '.$starttime.'<br>End Time: '.$endtime;
  
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
egoselfaxisAuthor Commented:
Works great - thanks!

- Yvan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.