How to check if the timestamp is today?

-3

I have a JSON obtained from an API that returns a list of events like this:

Array
(
    [alertList] => Array
        (
            [alerts] => Array
                (
                    [0] => Array
                        (
                            [id] => 145392039
                            [name] => Failed Zonetransfer 
                            [type] => Failed Zonetransfer (Critical)
                            [startDate] => Aug 22, 2016 10:24 AM
                            [errorString] => 
                        )
              )
            [errorString] => 
      )
)

I use strtotime at index startDate to format it for timestamp.

How can I check if the event is today?

I thought about comparing with the current day, taking the timestamp at 00:00, but it did not work:

$date  = new DateTime();
//$date->add(new DateInterval('PT1D')); essa linha tem erro, o parametro creio eu
$timestamp = ($date->getTimestamp()*1000);      
echo $timestamp.PHP_EOL;exit;
    
asked by anonymous 22.08.2016 / 17:14

2 answers

5

Just compare the date by formatting it with date :

if(date('d/m/y') == date('d/m/y', strtotime('Aug 22, 2016 10:24 AM'))) {
    echo "É hoje!"; // Ludmilla curtiu
} 
    
22.08.2016 / 18:03
0

Solution:

public function validAlert(array $alerts){
		$alertsValid = array();
		$today = new DateTime();		

		for($i=0; $i < count($alerts['alertList']['alerts']); $i++){
			$timestamp = strtotime($alerts['alertList']['alerts'][$i]['startDate']);
			$timestamp = date('d-m-Y', $timestamp);
			$date = new DateTime($timestamp);
			$diff = $date->diff($today);
			$interval = intval($diff->format('%a%'));
			if($interval === 0){
				array_push($alertsValid, $alerts['alertList']['alerts'][$i]);
			}		
		}//for
		return $alertsValid;
						
	}/*Final Function ValidAlert*/
    
22.08.2016 / 18:03