Establishing a time frame using AJAX through a separate PHP script

In order to display either a video or an image based on a specific time range, I want to utilize the client's local time by taking it from the website. Here is what I have done so far:

1st file (time.php):

<?php $b = time (); print date("H:i", $b); ?>

2nd file:

<script type="text/javascript">
setInterval(function() {
    $.ajax({
        url : "/time.php",
        type: 'GET',
        success: function(data){
            if($b >= 09:00 && $b < 10:00){ 
                document.getElementById("liveTV_api").style.display="block";
                document.getElementById("adtv").style.display="none";
            } else{
                document.getElementById("adtv").style.display="block";
                document.getElementById("liveTV_api").style.display="none";
            }
        }
    });
}, 1000);
</script>

I am encountering an issue and receiving an error in the browser console related to the time:

[ERROR] time 10:09:11.238 :: org.flowplayer.rtmp::SubscribingRTMPConnectionProvider : received onFCSubscribe

Any suggestions on how to resolve this?

Answer №1

To implement this functionality, consider using JSON in the following manner: The first file named time.php:

<?php 
$b = time(); 
echo json_encode(array('date'=>date("Y-m-d H:i:s", $b)); 
?>

The second file can be structured as follows:

<script type="text/javascript>
setInterval(function() {
    $.ajax({
        url : "/time.php",
        type: 'GET',        
        success: function(data){
            var cur_date=Date.parse("01-01-2014 "+data.date+":00"); // parse time
            var date1=Date.parse("01-01-2014 09:00:00");  
            var date2=Date.parse("01-01-2014 10:00:00");
            
            if(cur_date >= date1 && cur_date < data2){ 
                document.getElementById("liveTV_api").style.display="block"; 
                document.getElementById("adtv").style.display="none"; 
            } else { 
                document.getElementById("adtv").style.display="block"; 
                document.getElementById("liveTV_api").style.display="none";
            }
        }
    });
}, 1000);
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error code 1452: Laravel seed data integrity violation detected

Tables created: Users, Products, Orders, Order_Items for an ecommerce website. Each product should have an author (user). Facing an error with Artisan: Integrity constraint violation 1452 Cannot add or update a child row: a foreign key constraint fail ...

What do we need to ensure that the ajax server response is executed as intended in the jQuery ajax post function?

The jQuery script provided here returns the following string: '#prayer_date'.html("03/19/1968"); However, it remains unexecuted because the function does not interact with the data variable. To make sure the above result is executed within the ...

Facing challenges in PHP with setting headers and sending emails via cPanel

For PHP, I'm struggling to set the header for the from address and have tried various options: The correct code is provided below, but it doesn't seem to be attached to the header. Although the code appears to be default from, it doesn't c ...

Storing encrypted passwords securely in CakePHP

When working on hashing passwords, everything seems to be running smoothly. However, when it comes to inserting or updating the data in a MySQL database, only the hashed password must be saved. Within Controller.php (calling the following method within ad ...

Creating a 16-bit string from an array in Python

I'm facing a challenge with packing an array to 16-bit depth using Python. In PHP, I have successfully achieved this without any problems. The array consists of numbers such as: [1, 2, 3, 4, 5, 700, 540...]. With PHP, I can accomplish this task in ju ...

Load additional content seamlessly using Ajax in Django without needing to reload the entire navigation bar

I recently started working with Django as I develop my own website. I have created a base.html file which includes the main body, CSS, js, fonts, and navbar that will be present on all pages of my site. The navbar is located in another HTML file called nav ...

"Converting SQLite to MySQL with PHP: A Step-by-Step Guide

After successfully converting my sqlite database to mysql, I now need to convert the corresponding php code as well. Do you happen to have any documentation or resources that outline the methods for making this conversion from sqlite to mysql? Your help is ...

Contrast between gmaps and traditional cross-domain AJAX queries

I am curious about the behavior of the getJSON method when making an ajax call to specific addresses. For example, when I make a call to "", no errors are generated. However, if I try to call an external domain that is not Google, the browser throws an e ...

C# ASP.NET Dynamic Gridview Expansion

Has anyone found a method to implement a collapsible ASP.NET gridview that can show parent-child relationships when a checkbox is clicked? ...

Javascript function to deselect all items

One of my functions is designed to reset all checkbox values and then trigger an AJAX request. However, there are instances when the function initiates before the checkboxes have been unchecked. function clear() { $("#a").prop("checked", false); $("#b ...

Optimizing WordPress Titles with PHP Wordwrap

Is there a way for PHP to automatically count the characters in my WordPress post title? I would like it to insert a line break before the 50th character. Would using wordwrap be the most effective solution for this task? I have tried implementing the co ...

Refreshing the site to submit the form

I am facing an issue with the page reloading every time I click submit on the form. Whenever I hit send, the form reloads and takes me to http://localhost:3000/mail.php. I would prefer if the site did not reload (I am using a jQuery validation form plugin) ...

Utilizing ajax to render partial templates with JSON responses

I am attempting to display a partial by calling an ajax function and then rendering it in a specific div element. However, when I use request.responseText in the code below, the HTML content that I desire is actually returned within the error function ins ...

The 'dataColumn' column is not assigned to the table: Employing Ado.Net Datatable with jQuery's $.ajax()

Currently, I am utilizing MVC5 with Ado.net datatable. Within a Controller, there is a method that I wish to invoke using an ajax call in order to send form data. My objective is to execute this method within the create action when adding new records, enab ...

Caution: Utilizing File_Get_Contents function in WordPress with PHP 8.1

I recently upgraded the PHP version from 7.4 to 8.1 and encountered an error while trying to post content in WordPress. Despite the error, the article still gets posted, but it's really bothering me. I enabled the WordPress debugger and found out tha ...

Missing session settings across all pages in PHP5

At the beginning of my login page, I include session_start(); to initiate a session. Once a user logs in, a message appears on the screen indicating that the session is being set. However, I am facing an issue where I am unable to carry sessions from one ...

Is this conditional statement accurate?

Could this be a legitimate condition? Why isn't it functioning as expected in PHP? var myString = 'hello'; if(myString == ('hello' || 'hi' || 'bonjour' || 'hallo')){ alert('Welcome'); } ...

When selecting filter options, the posts/images now overlap before transitioning into a masonry view upon resizing the window

I have implemented a filter on my posts to arrange them based on popularity, style, and country. Screenshots: Screenshot 1: Everything looks good when the page loads, displaying all posts/images properly in masonry layout. Screenshot 2: Issue occurs whe ...

Having trouble removing a document from SOLR using PHP

I have encountered an issue with deleting documents based on multiple attributes. Previously, I was able to successfully delete documents from Solr using only the 'id' field. Now, I need to remove documents based on two fields. For example, if I ...

Combining two columns to generate a query for account creation flow

There are two tables: one for client credit (payments) and another for debit (withdrawals). The payments table looks like this: id client date amount 1 ana 2012-01-01 5000 2 ana 2012-02-01 10000 ...