ExpressiveDate::makeFromDateTime PHP Method

makeFromDateTime() public static method

Make and return a new ExpressiveDate instance with defined year, month, day, hour, minute, and second.
public static makeFromDateTime ( integer $year = null, integer $month = null, integer $day = null, integer $hour = null, integer $minute = null, integer $second = null, string | DateTimeZone $timezone = null ) : ExpressiveDate
$year integer
$month integer
$day integer
$hour integer
$minute integer
$second integer
$timezone string | DateTimeZone
return ExpressiveDate
    public static function makeFromDateTime($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null)
    {
        $date = new static(null, $timezone);
        $date->setDate($year ?: $date->getYear(), $month ?: $date->getMonth(), $day ?: $date->getDay());
        // If no hour was given then we'll default the minute and second to the current
        // minute and second. If a date was given and minute or second are null then
        // we'll set them to 0, mimicking PHPs behaviour.
        if (is_null($hour)) {
            $minute = $minute ?: $date->getMinute();
            $second = $second ?: $date->getSecond();
        } else {
            $minute = $minute ?: 0;
            $second = $second ?: 0;
        }
        $date->setTime($hour ?: $date->getHour(), $minute, $second);
        return $date;
    }

Usage Example

Beispiel #1
0
 public function testDateIsCreatedFromDateTime()
 {
     $date = ExpressiveDate::makeFromDateTime(2013, 1, 31, 8, null, null);
     $this->assertEquals('2013-01-31 08:00:00', $date->getDateTime());
     $date = ExpressiveDate::makeFromDateTime(2013, 1, 31, -12, null, null);
     $this->assertEquals('2013-01-30 12:00:00', $date->getDateTime());
 }