public function testGetTimestamp() { $_POST['date'] = '12/10/2009'; $this->assertEquals('12/10/2009 12:13:14', date('d/m/Y H:i:s', $this->txtDate->getTimestamp(null, null, null, 12, 13, 14))); $this->assertEquals('12/10/2010 12:13:14', date('d/m/Y H:i:s', $this->txtDate->getTimestamp(2010, null, null, 12, 13, 14))); $this->assertEquals('12/11/2009 12:13:14', date('d/m/Y H:i:s', $this->txtDate->getTimestamp(null, 11, null, 12, 13, 14))); $this->assertEquals('25/10/2009 12:13:14', date('d/m/Y H:i:s', $this->txtDate->getTimestamp(null, null, 25, 12, 13, 14))); $_POST['date'] = array('foo', 'bar'); $this->assertEquals(date('Y-m-d H:i:s'), date('Y-m-d H:i:s', $this->txtDate->getTimestamp())); }
/** * Get the UTC timestamp for a date/time object combination. * * @param \SpoonFormDate $date An instance of \SpoonFormDate. * @param \SpoonFormTime $time An instance of \SpoonFormTime. * @return int * @throws \Exception If provided $date, $time or both are invalid */ public static function getUTCTimestamp(\SpoonFormDate $date, \SpoonFormTime $time = null) { // validate date/time object if (!$date->isValid() || $time !== null && !$time->isValid()) { throw new \Exception('You need to provide two objects that actually contain valid data.'); } // init vars $year = gmdate('Y', $date->getTimestamp()); $month = gmdate('m', $date->getTimestamp()); $day = gmdate('j', $date->getTimestamp()); if ($time !== null) { // define hour & minute list($hour, $minute) = explode(':', $time->getValue()); } else { // user default time $hour = 0; $minute = 0; } // make and return timestamp return mktime($hour, $minute, 0, $month, $day, $year); }