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

18th Oct 2020, 2:11 PM
ابو العز
ابو العز - avatar
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 }
18th Oct 2020, 5:01 PM
Ore
Ore - avatar
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>"; }
18th Oct 2020, 6:21 PM
JaScript
JaScript - avatar
0
Here the live example how to control output by time with PHP code: https://code.sololearn.com/w2yqPJJ8ew39/?ref=app
19th Oct 2020, 9:05 PM
JaScript
JaScript - avatar