How to exchange one image for another depending on the time?

Asked

Viewed 44 times

0

I’m pulling the rss of instagram of a market profile, but I want it at 10:00 in the morning 00:00 (midnight) it displays the photo of the first post, and that from the 00:01 until 9:59 in the morning it displays any illustration image.

I’m trying to use the non-server time so it doesn’t go wrong. What I tried so far worked only the display during the day, but when midnight comes it displays the error message. That is the code:

<?php
$url = "https://www.instagram.com/comprebemsupermercado/?__a=1";
$data = file_get_contents($url);
$arrData = json_decode($data, true);
$instagram_photos = $arrData['graphql']['user']['edge_owner_to_timeline_media']['edges'][0]['node']['display_url'];

echo "<meta http-equiv='refresh' content='1800'; url='index.php'>"; //atualiza a cada 30 minutos
echo "<link rel='stylesheet' type='text/css' href='style.css?version=330'>";
echo "<div class='fundo'>";
echo "</div>";

// DEFINE O FUSO HORARIO COMO O HORARIO DE BRASILIA
    date_default_timezone_set('America/Sao_Paulo');
// CRIA UMA VARIAVEL E ARMAZENA A HORA ATUAL DO FUSO-HORÀRIO DEFINIDO (BRASÍLIA)
    $hr = date(' H:i ', time());

if($hr >= 24 && $hr < 10) {
$resp = "<img class='logo' src='logo.jpg' alt=''/>";}
else if ($hr >= 10 && $hr < 24 ){
$resp = "<img class='img-responsive' src='{$instagram_photos}'/>";}
else {
$resp = "Erro";}
echo "$resp";
?>

And this is the CSS:

.img-responsive{
    position: absolute;top: 0;
    left: 200px;
    top: 100px;
    width: 900px;
    height: 900px;
}
.fundo{
    position: absolute;top: 0;
    width: 1920px;
    height: 1080px;
}
.logo{
    position: absolute;top: 0;
    left: 200px;
    top: 100px;
    width: 900px;
    height: 900px;
}

1 answer

1


I’m pulling the rss instagram a market profile, but I want the 10:00 from morning until 00:00 (midnight) it displays the photo of the first post, and that from the 00:01 until 9:59 in the morning it displays any illustration image.

Missed the part of time knowing where the time starts 00:00 and ends in the 23:59 (it doesn’t have 24:00), then:

<?php

    //G Formato 24-horas de uma hora sem zero à esquerda (0 até 23)
    $hour = (int)date('G');
    if ($hour >= 0 && $hour < 10)
    {
        // 00:00 até 09:59
    }
    else 
    {
        // 10:00 até 23:59
    }

Reference: Function date

  • A tip dear Virgilio, the cast (int) is dispensable in this type of comparison, php itself does it, I think it would be only interesting if the goal was to achieve micro-optimization, as a loop that will process about 40,000 comparisons like this in "ifs". ;)

  • a hint if I want a comparison with the text type neh @Guilhermenascimento and if I want to force the type to be a number, because it returns a text type ...

  • But the cast or comparison with operators of smaller and larger will be the same effect to compare numbers, understand? I’m talking about the specific case and not "all" types of comparison ;)

  • But I want so @Guilhermenascimento ... I want him to do the cast. One comes and speaks in loose comparison, another comes and contexts, here no one is satisfied in wisdom.

  • @Virgilionovic gave error in the two points between the hourly numbers

  • It depends on what you did @jose113 ??? how you did it and what went wrong!

  • date_default_timezone_set('America/Sao_paulo'); // CREATES A VARIABLE AND STORES THE CURRENT TIME OF THE DEFINED TIME ZONE (BRASÍLIA) $hour = (int)date('G'); if ($hour >= 0 && $hour < 09:59){ $Resp = "<img class='logo' src='logo.jpg' alt='/>"} Else if ($hour >= 10:00 && $hour < 0:00 ){ $Resp = "<img class='img-Responsive' src='{$instagram_photos}'/>";} Else { $Resp = "Error";} echo "$Resp"; ;?>

  • looks like you wrote $hour >= 0 && $hour < 09:59 just put <10 if you didn’t follow my reply @jose113 o if ta pronto é só colocar as duas imagens dentro do cochetes

  • @Virgilionovic Yes, even now following your answer did not work

Show 5 more comments

Browser other questions tagged

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