diff --git a/includes/class-Date.php b/includes/class-Date.php new file mode 100644 index 0000000..b3008b4 --- /dev/null +++ b/includes/class-Date.php @@ -0,0 +1,66 @@ +. + +namespace itwikidelbot; + +use DateTime; + +/** + * Handle a human date + */ +class Date { + + /** + * Get a DateTime object from a human date + * + * @param $category_name Category name e.g. '4 aprile 2013' + * @return DateTime + */ + public static function human2object( $human_date ) { + $parts = explode( ' ', $human_date ); + if( 3 === count( $parts ) ) { + list( $d, $human_m, $y ) = $parts; + $m = Months::name2number( $human_m ) + 1; + return new DateTime( sprintf( + // yyyy-mm-dd + '%s-%02d-%02s', + $y, + $m, + $d + ) ); + } + throw new \InvalidArgumentException( 'wrong human date' ); + } + + /** + * Get a human date from a DateTime object + * + * @param $datetime DateTime + * @return string e.g. '4 aprile 2013' + */ + public static function object2human( DateTime $datetime ) { + list( $y, $m, $d ) = explode( ' ', $datetime->format('Y m d') ); + $human_month = Months::number2name( $m - 1 ); + return "$d $human_month $y"; + } +}