Link to home
Start Free TrialLog in
Avatar of egoselfaxis
egoselfaxis

asked on

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
 

Avatar of Hube02
Hube02
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of egoselfaxis
egoselfaxis

ASKER

Works great - thanks!

- Yvan