Oct 1-12 ,Fri 12:22 のフォーマット済みの日付を日付型に逆フォーマットするやつ

 
date_default_timezone_set('America/Los_Angeles');
// 計算するので現地の時間でやる
$usweek = ['Sunday'=>0,'Monday'=>1,'Tuesday'=>2,'Wednesday'=>3,'Thursday'=>4,'Friday'=>5,'Saturday'=>6];
$usmonth = ['Jan'=>1,'Feb'=>2,'Mar'=>3,'Apr'=>4,'May'=>5,'June'=>6,'July'=>7,'Augt'=>8,'Sept'=>9,'Oct'=>10,'Nov'=>11,'Dec'=>12,];

$ending = 'Friday at 11:21PM PDT';  //  入力例1
$ending = 'Oct 12 at 5:14PM PDT';   //      2
$ending = 'Today at 11:57PM PDT';   //      3

$endings = explode(" ", $ending);
$now = new DateTime();

if( array_key_exists( $endings[0] ,$usweek )){
  // 曜日から始まってる
    $apm = substr($endings[2], -2);
    $hmt = str_replace($apm ,'' , $endings[2]);
    $w = date('w', $now->getTimestamp());  //今日の曜日  4 
    $deff = $usweek[$endings[0]] - $w;    //1 日後
    echo date("Y-m-d",strtotime("+$deff day"))," " , date('H:i',strtotime($endings[2])),"
";   //17:21 ok

}elseif(array_key_exists( $endings[0] ,$usmonth )){
    $endday = $usmonth[$endings[0]].'-'. $endings[1].' '. date('H:i',strtotime( $endings[3]));
    if(date("m-d H:i") < $endday){
      echo date("Y") .'-'.$endday;
    }else{ 
      echo date("Y",strtotime("+1 year")).'-'.$endday;
    }
}elseif( $endings[0] == 'Today'){
    echo date("Y-m-d")." " . date('H:i',strtotime($endings[2])) ;
}

 

Categories:

Tags: