How to check if the timestamp is today?

Asked

Viewed 343 times

-4

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 in the Indian startDate to format it to timestamp.

How can I check if the event is from today?

I thought I’d compare it to the present day, picking up the timestamp at 12:00, but it didn’t 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;
  • Post a return model.

  • 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 give strtotime in startDate Indice to format for timestamp

  • I just need to know, is it today, day 22? the time I believe start at 00:00

  • Or I needed to pick up today’s timestamp at 00:00 am I’m already using it: $date = new Datetime(); //$date->add(new Dateinterval('PT1D')); this line has error, the parameter believe me $timestamp = ($date->getTimestamp()*1000); echo $timestamp.PHP_EOL;Exit; I’m multiplying by 1000 pq is in milliseconds.

  • 1

    Daniel please put the Json return format and a minimum of code so we know what you have already done.

3 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
} 

-1

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*/

-1

I do so:

$dt = new DateTime();
$dt2 = new DateTime();
$dataAtual =  $dt->getTimestamp();

$dt2->setTimestamp(145392039);
if($dt->format('d') == $dt2->format('d'))
{
    echo 'É hoje';
}
else
{
    echo 'Não é hoje';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.