gpt4 book ai didi

php - 每天更换一个DIV之间的内容

转载 作者:可可西里 更新时间:2023-10-31 22:58:40 26 4
gpt4 key购买 nike

我需要一个 JS 来每年更改这两个 div 的内容。例如:在 1 月 1 日,date div 将是“JAN 1”,而 moto div 将是 1 月 1 日的一些 moto,然后在 1 月 2 日,日期和 moto 发生变化,但不是随机的,我在 365 天的每一天都有文本。所以只是一个例子,我可以填写其余部分。我以为是 JS,但如果更容易的话,可以在服务器端使用 PHP。

<div id="date"><h3>JAN 1</h3></div>
<div id="moto" align="center">Example Text.</div>

最佳答案

为了满足您的要求,我提供了一个解决方案...不过就像其他答案所说的那样:最好在服务器端完成。

<script>
var date = new Date(); //get current date
var month = date.getMonth();
var day = date.getDate();
var months = ['JAN', 'FEB', ... , 'DEC']; //array of month formats

//define mottos as a multi-dimensional array by month and day
var mottos = [
[
'JAN 1 Motto',
'JAN 2 Motto',
...,
'JAN 31 Motto'
],

...,

[
'DEC 1 Motto',
...,
'DEC 31 Motto'
]
];

//set the Date DIV
document.getElementById('date').innerHTML = months[month] + " " + day;

//set the Motto DIV; the day - 1 accounts for the zero-indexed arrays
document.getElementById('moto').innerHTML = mottos[month][day - 1];
</script>

使用 PHP:

<?php

//Similar to the above JS method
$mottos = array(
array(
'Jan 1 Motto',
...
),
...
);

$date = new DateTime();
$month = (int) $date->format('n');
$day = (int) $date->format('j');
?>

<div id="date"><h3><?php echo strtoupper($date->format('M j')); ?></h3></div>
<div id="moto" align="center"><?php echo $motto[$month - 1][$day - 1];</div>

关于php - 每天更换一个DIV之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6553523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com