<?php
namespace App\Helpers;

/**
* Class based Helper to upload
*/
class DateDiffHelper
{
    public static function diff($dateFrom, $dateTo= null){
        $dateTo = new \DateTime($dateTo ?? date('Y-m-d H:i:s'));
        $dateFrom = new \DateTime($dateFrom);
        // $dateDiff = $dateFrom->diff($dateTo);
        $dateDiff = $dateTo->diff($dateFrom);
        $formatted= [
            'date'=> __('main.'.$dateFrom->format('F')).$dateFrom->format(' d, Y'),
            'time' => $dateFrom->format(' g:i ').__('main.'.$dateFrom->format('A'))
        ];
        $date= $formatted['date'];
        $time= $formatted['time'];

        if ($dateTo->format('Y-m-d') == $dateFrom->format('Y-m-d')) {
            $date= __('main.today');
        }elseif ($dateDiff->h > 0 && $dateDiff->d == 0 && $dateFrom->getTimestamp() < $dateTo->getTimestamp() ) {
            $date= __('main.yesterday');
        }elseif ($dateDiff->days <= 29) {
            $date= __('main.since').$dateDiff->days .__('main.day');
        }

        if ($dateTo->format('Y-m-d') == $dateFrom->format('Y-m-d')) {
            if($dateDiff->h == 0 && $dateDiff->i == 0 ){
                $time= $dateDiff->s<=10 ? __('main.now') : (__('main.since'). $dateDiff->s . __('main.second'));
            }elseif($dateDiff->h == 0 && $dateDiff->i == 1 && $dateDiff->s<=15){
                $time= __('main.since'). __('main.minute') ; 
            }
        }
         // to display time as value not as relative to current time
        $time= $dateFrom->format(' g:i ').__('main.'.$dateFrom->format('A'));
        $seconds= $dateTo->getTimestamp() - $dateFrom->getTimestamp();
        $label= $seconds < 86400 ? ($seconds < 60 ? __('main.now') : $time) : $date;
        return ['date'=> $date, 'time'=> $time, 'label'=> $label, 'formatted'=> $formatted, 'seconds'=> $dateTo->getTimestamp() - $dateFrom->getTimestamp()];
        // dd($dateFrom->format('Y-m-d H:i:s'), $dateTo->format('Y-m-d H:i:s'),$date, $time);
    }
}
?>