0
PHP. Hello I want to display a product during the morning period, and at the end of the morning, another product is displayed d
Hello I want to display a product during the morning period, and at the end of the morning, another product is displayed during
3 ответов
+ 1
There is the date() function which tells the date according to the server time.
It has two parameters.
1. string: The format
https://www.php.net/manual/en/datetime.format.php
2. int: The timestamp, defaults to the current unix timestamp
date('a') returns 'am' or 'pm' according to the current server time. Are you surprised? If you are, look at this link again
https://www.php.net/manual/en/datetime.format.php
But keep in mind that date() uses server location timezone as configured in php.ini settings.
date('a') === 'am' ? p1 : p2;
If you want to use user's timezone. You need an extension like GeoIP. It comes with the php installation. Don't do it! Just use JavaScript to check the user's time in the front end.
So my suggestion is : send the two products to the front end with display of none. When page loads, use JavaScript to show the correct one.
const date = new Date();
if (date.getHours < 12) {
// am
} else {
//pm
}
0
The easiest way will be as follows:
echo "<p>" . date("d.m.Y, H:i:s") . "</p>"; // only for current time information
if(time() > strtotime('18-10-2020 17:04:33') and time() < strtotime('18-10-2020 18:14:53')){
echo "<p><b> I am standing here because the current date is between above mentioned time borders.</b></p>";
}
0
Here the live example how to control output by time with PHP code:
https://code.sololearn.com/w2yqPJJ8ew39/?ref=app