Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@heiglandreas
Last active July 4, 2018 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heiglandreas/ad1bee777ffbc4797fc73ce4af934893 to your computer and use it in GitHub Desktop.
Save heiglandreas/ad1bee777ffbc4797fc73ce4af934893 to your computer and use it in GitHub Desktop.
A plain Date-Object without time-information – somehow
<?php
/*
* Copyright (c) Andreas Heigl <andreas@heigl.org>
*
* Licensed under the MIT License. See LICENSE.md file in the project root
* for full license information.
*/
namespace Org_Heigl\Date;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
class Date
{
private $datetime;
public function __construct($date, DateTimeZone $timezone = null)
{
$this->datetime = new DateTimeImmutable($date, $timezone);
if (null !== $timezone) {
$this->datetime->setTimezone($timezone);
}
$this->datetime = $this->datetime->setTime(12,00,00,0);
}
public function format($format) : string
{
// prepend all time-relevant information from the formatting string with
// backslashes to escape them. THis format can only format date-information
$this->datetime->format($format)
}
public function add(DateInterval $interval) : Date
{
// Remove all time-information from the interval. So everything behind the "T"
$self = clone($this);
$self->datetime->add($interval);
return $self;
}
public function sub(DateInterval $interval) : Date
{
// Remove all time-information from the interval. So everything behind the "T"
$self = clone($this);
$self->datetime->sub($interval);
return $self;
}
public function modify(string $modification) : Date
{
// Remove all time-information from modification-string
$self = clone($this);
$self->datetime->modify($modification);
return $self;
}
public function diff(Date $date) : DateInterval
{
$interval = $this->datetime
->setTimezone(new DateTimeZone('UTC'))
->setTime(12,0,0,0)
->diff(
$date->datetime
->setTimezone(new DateTimeZone('UTC')=
->setTime(12,0,0,0)
);
// Remove all time-relevant informations from DateInterval (shouldn'T be there anyway)
return $interval;
}
public function getTimezone() : DateTimeZone
{
return $this->datetime->getTimezone();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment