<?php

namespace EesyPHP;

class Date {

  /**
   * The date format
   * @see strftime()
   * @var string
   */
  public static $date_format = "%d/%m/%Y";

  /**
   * The datetime format
   * @see strftime()
   * @var string
   */
  public static $date_time_format = "%d/%m/%Y %H:%M:%S";

  /**
   * Format a timestamp as date/datetime string
   * @param int $time The timestamp to format
   * @param bool $with_time If true, include time in formated string
   *                        (optional, default: true)
   */
  public static function format($time, $with_time=true) {
    if ($with_time)
      return strftime(self :: $date_time_format, $time);
    return strftime(self :: $date_format, $time);
  }

  /**
   * Parse a date/datetime string as timestamp
   * @param string $date The date string to parse
   * @param bool $with_time If true, consider the date string included time
   *                        (optional, default: true)
   */
  public static function parse($date, $with_time=true) {
    if ($with_time)
      $ptime = strptime($date, self :: $date_time_format);
    else
      $ptime = strptime($date, self :: $date_format);
    if(is_array($ptime)) {
      return mktime(
        $ptime['tm_hour'],
        $ptime['tm_min'],
        $ptime['tm_sec'],
        $ptime['tm_mon']+1,
        $ptime['tm_mday'],
        $ptime['tm_year']+1900
      );
    }
    return false;
  }
}
