简体   繁体   English

使用 PHP 和 HTML 制作动态文本

[英]Making Dynamic Text With PHP and HTML

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;我对 PHP 很陌生,我希望主页显示; '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雷诺

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您可以通过简单地更改 if 中的值来更改时隙

What you need to do here is to get the system time first, then print the message you want after a condition check.这里需要做的是先获取系统时间,然后在条件检查后打印你想要的消息。 Here is a 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;

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).您检查date()中的当前小时数(H 是 24 小时格式的当前小时数)是否大于或等于 17 以获得晚上(您可以根据自己的喜好随意更改)。 If not check if it is greater than or equal to 12 for the Afternoon.如果不是,请检查下午是否大于或等于 12。 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!您可以移动Good <?php echo $greeting; ?> Reno! 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.在您的 HTML 页面中的任何位置行,只需确保$greeting代码位于页面顶部或至少在问候行上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM