ks465 / yii2-utils

A pack of multiple components and widgets, especially designed for simpler and more generic coding
GNU General Public License v2.0
0 stars 0 forks source link

Jalali #42

Closed ks465 closed 5 years ago

ks465 commented 5 years ago

Add a method to create timestamp from date string, and one to return the date string in standardize format:

time($dateString){
   $rx = '~^(\d{2,4})\D(\d{1,2})\D(\d{1,2})$~';
   preg_match($rx, $dateString, $segments);;
   return Jalali::mktime($segments[1], $segments[2], $segments[3]);
}
std($dateString){
   return Jalali::date(STD_FORMAT, time($dateString));
}
ks465 commented 5 years ago

The complete methods are:

* @version    3.3.1-980217
/**
     * Convert a typical date string into timestamp
     *
     * @param string $dateString
     *
     * @return int
     */
    public static function getTimestampFromString($dateString)
    {
        if(empty($dateString)){
            return null;
        }
        $rx = '~^(\d{2,4})\D(\d{1,2})\D(\d{1,2})$~';
        preg_match($rx, $dateString, $segments);;

        return Jalali::mktime($segments[1], $segments[2], $segments[3]);
    }

    /**
     * Rewrites a date string in standardize format based on the given format
     *
     * @param string $dateString typical date string
     * @param string $format the output format. Defaults to [Jalali::KHAN_SHORT]
     *
     * @return mixed
     */
    public static function standardize($dateString, $format = Jalali::KHAN_SHORT)
    {
       if(empty($dateString)){
            return null;
        }
        return Jalali::date($format, time($dateString));
    }
ks465 commented 5 years ago

Done