Home > OS >  Making Dynamic Text With PHP and HTML
Making Dynamic Text With PHP and HTML

Time:01-07

I have a question, I am making some sort of personal assistant site for school and stuff. I am very new to PHP and I want that the homepage displays; 'Goodmorning Reno!'

I got that already but now I want to make it dynamic so when it is Morning it says; Goodmorning and if it is Noon it says; Good afternoon.

My question is how do I do this because im at a loss.

And if it is not possible than I would love other ideas.

Thanks,

Reno

CodePudding user response:

This code i think thath will help you, if i current understand

code:

<?php
$hour = date('H');
if($hour >= "13"){
    echo("Goodafternoon Reno!");
}else{
    echo("Goodmorning Reno!");
}
?>

You can change the time slot by simply changing the value in the if

CodePudding user response:

@Rummy03, burada yapman gereken öncelikle sistem saatini almak, daha sonra bunu bir şart kontrolü sonrası istediğin mesajı yazdırmak. örnek kod istersen;

translate

What you need to do here is to get the system time first, then print the message you want after a condition check. If you want sample code;

    <?php


$saat = date("h");

$mesaj = null;

if ( $saat > 6 and $saat < 12 ){
    
    $mesaj = "İyi Sabahlar";

}elseif( $saat > 12 and $saat < 18 ){
    
    $mesaj = "iyi günler-öğlenler";

}elseif( $saat > 18 and $saat < 21 ){
    
    $mesaj = "iyi geceler";
}

echo  $mesaj;

CodePudding user response:

There are a few ways of handling this but here is mine.

You check to see if the current hour from date() (H is for the current hour in 24-hour format) is greater than or equal 17 to get the evening (feel free to change this for your own preference). If not check if it is greater than or equal to 12 for the Afternoon. Everything else must be morning!

<?php
$currentHour = date('H');

if($currentHour >= 17) {
    $greeting = "Evening";
} elseif($currentHour >= 12) {
    $greeting = "Afternoon";
} else {
    $greeting = "Morning";
}
?>

Good <?php echo $greeting; ?> Reno!

Just for reference, you can also create the greeting within a ternary statement by doing the following

$greeting = ($currentHour >= 17) ? "Evening" : ($currentHour >= 12) ? "Afternoon" : "Morning");

But as you are new to this, I suggest learning the easier to ready way first then moving on to ternary.

You can move the Good <?php echo $greeting; ?> Reno! line anywhere within your HTML page, just make sure the $greeting code is at the top of the page or at least above the greeting line.

  •  Tags:  
  • Related