0

I have a little logic problem. I have this:

$y=2014;
$m=05;
$d=22;
$d2=$d+1;

$sunset=17:52;
$sunrise=05:44;

I have some events that occurs during the days 22 and 23, but I want select only the ones that happen between $sunset and $sunrise.

Ho can I say something like this:

if($event [$sunset<BETWEEN>$sunrise]){
//show something;
}

? Thanks in advance.

2 Answers 2

2

Use DateTime() as they are comparable which makes this easy:

$sunrise = new DateTime('2014-05-22 05:44');
$sunset  = new DateTime('2014-05-23 17:52');
$good    = new DateTime('2014-05-22 18:00');
$bad     = new DateTime('2014-05-23 18:00');
if ($good > $sunrise && $good < $sunset) {
    echo '$good is good';
}
if ($bad > $sunrise && $bad < $sunset) {
    echo '$bad is good';
}

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You should create DateTime objects and then it's as simple as calculating the difference in time.

http://www.php.net/manual/en/datetime.diff.php

Example:

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

1 Comment

I was thinking, now, that I can work with the julian day too, may be can be easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.