XF 2.0 Both Date and Time inputs?

It'll always pass the value in 24 hour format, and I believe your browser will determine whether it shows on the front end as 24 or 12 hour format, perhaps based on your location. Not 100% sure, mine shows up in 12 hour format, but I'm not sure if that's something you can change to use a user preference (or board preference) or if it's just what the browser wants to do
 
Final code:

PHP:
        $dateInput = $this->filter([
            'date' => 'datetime',
            'time' => 'str',
        ]);
        $form->setup(function() use ($dateInput, $download)
        {
            $language = \XF::language();

            $dateTime = new \DateTime('@' . $dateInput['date'], $language->getTimeZone());

            if (!$dateInput['time'] OR strpos($dateInput['time'], ':') === false)
            {
                // We didn't have a valid time string
                $hours = $language->date($download->release_date, 'H');
                $minutes = $language->date($download->release_date, 'i');
            }
            else
            {
                list($hours, $minutes) = explode(':', $dateInput['time']);

                // Sanitise hours and minutes to a maximum of 23:59
                $hours = min(intval($hours), 23);
                $minutes = min(intval($minutes), 59);
            }

            // Finally set it
            $dateTime->setTime($hours, $minutes);

            $download->release_date = $dateTime->getTimestamp();
        });

Either of you see any immediate issues with that? I changed the input type to a text so I could experiment some more. Tried with a : but missing hours and minutes, as well as leaving the : out, and it seems to work.


Fillip
Why do all that code and not just stick to a simple STRTOTIME?
 
Hmm... that makes sense. I'm having a time zone issue...

Code:
        $date = $this->filter('date', 'datetime');
        $time = $this->filter('time', 'str');
        list ($hour, $min) = explode(':', $time);
      
        $dateTime = new \DateTime('@' . $date, \XF::language()->getTimeZone());
        $dateTime->setTime($hour, $min);
        $input['article_date'] = $dateTime->getTimestamp();

        echo $dateTime->format('Y-m-d H:i:s e');

My timezone is -05:00 (America/New_York, EST).

Inputting the date "2017-12-16" "01:31 PM" is returning:
Code:
2017-12-16 13:31:00 +00:00

As you can see, its not respecting the timezone.
 
Last edited:
So both:
Code:
$dateTime = new \DateTime('@'.$date, \XF::language()->getTimeZone());
and:
Code:
$dateTime = new \DateTime('@'.$date);
Are giving me the same result... no matter what timezone I have.



Here is a printout of date/hour/min/timezone and the result:
$date = 1513400400
$hour = 13
$min = 31
$timezone = DateTimeZone {#277 ▼ "timezone_type": 3 "timezone": "America/New_York" }

And this is the result:
Code:
2017-12-16 13:31:00 +00:00

As you can see... timezone getting lost. So if the reason to use DateTime is to respect timezone... and its not respecting the timezone...
 
Last edited:
Okay, if I move the timezone out of the constructor, it seems to work fine:
Code:
            $dateTime = new \DateTime('@'.$date);
            $dateTime->setTimeZone(\XF::language()->getTimeZone());
            $dateTime->setTime($hour, $min);

Why? I have no idea.
 
Top Bottom