ultrarun
ultrarun

Reputation: 274

PHP Timestamp if before noon

I'm trying to print a specific mesasge to the browser depending if my timestamp is AM or PM. In this instance if it's before 12 noon then then I want to echo out Good morning.

The time stamp held in $person['StartDateTime'] is 2019-10-09T14:00:00

The message always prints out the good morning message and I've tried the other posts on here but can't seem to get it to work. I've tried 'noon', 12, 12 PM, etc..

if ($person['StartDateTime'] < strtotime("noon"))
    {
    echo 'Meeting Time:' . $person['StartDateTime'] . '<br>';
    echo "Good morning" . '<br>';
    }

Edited:

If I try as suggested below I get nothing back.

if (strtotime($person['StartDateTime']) < strtotime("noon"))

If I try using after >

if (strtotime($person['StartDateTime']) > strtotime("noon"))

I get the message but also on my morning timestamp - 2019-10-09T10:00:00 which I shouldn't.

I'm wondering do I need to specify noon for the day of the timestamp?

Upvotes: 1

Views: 453

Answers (3)

Robert
Robert

Reputation: 20286

I suggest using Datetime with proper format argument a which gives either am or pm depending on the time

if ((new Datetime($person['StartDateTime']))->format('a') == 'am') {
  // am
} else {
  // pm
}

Upvotes: 2

ultrarun
ultrarun

Reputation: 274

Ok after trying a few things the solution in this case was simple.

Format the date using only "A", this will return AP or PM depending on the time.

$starttime = date("A",strtotime(date($person['StartDateTime'])));

if ($starttime == "AM"){
echo "Good Morning" . '<br>';
}

Upvotes: 2

Eugene
Eugene

Reputation: 1730

strtotime return timestamp. You compare int value with string. Read about strtotime function

Upvotes: 0

Related Questions