4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

24時間形式←→29時間形式 日付変換関数(主に放送業界用)

Last updated at Posted at 2017-05-04

需要があるかどうか分かりませんが、某テレビ局さんの、とあるプロジェクトに参画させて頂いていた際に作ったのでここに記しておきます。
動くサンプルはこちら

24時間形式を29時間形式へ

<?php
    /**
     * @param  string $datetime
     * @param  bool $isEnd
     * @return string
     */
    public function convert24DateTo29Date(string $date, string $time = '00:00:00', bool $isEnd = false): string
    {
        /**
         * 00:00:00 ~ 04:59:59 または、終了時間かつ 05:00:00 なら29時間形式へ変換
         */
        if ( ("{$date} 00:00:00" <= "{$date} {$time}" && "{$date} {$time}" < "{$date} 05:00:00")
            || ( $time === "05:00:00" && $isEnd ) )
        {
            $arrDate = explode('-', $date);
            $arrTime = explode(':', $time);
            $timestamp = mktime(0, 0, 0, $arrDate[1], $arrDate[2], $arrDate[0]);
            return date('Y-m-d', $timestamp - 60 * 60 * 24) . ' ' . str_pad(intval($arrTime[0]) + 24, 2, 0, STR_PAD_LEFT) . ":{$arrTime[1]}:{$arrTime[2]}";
        }
        
        return "{$date} {$time}";
    }

29時間形式を24時間形式へ

<?php
    /**
     * @param  string $date
     * @param  string $time
     * @return string
     */
    public function convert29DateTo24Date(string $date, string $time = '00:00:00'): string
    {
        $arrTime = explode(':', $time);
        
        if (intval($arrTime[0]) >= 24 && intval($arrTime[0]) <= 29) {
            $arrDate = explode('-', $date);
            $timestamp = mktime(intval($arrTime[0]) - 24, $arrTime[1], $arrTime[2], $arrDate[1], $arrDate[2], $arrDate[0]);
            return date('Y-m-d H:i:s', $timestamp + 60 * 60 * 24);
        }
        
        return "{$date} {$time}";
    }
4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?