Comparing two timestamps to the current date in PHP using Laravel

Comparing two timestamps, starting_date and ending_date, with the current time is a necessity for me.

I am trying to achieve the following:

 $discount_db = Discount::whereActive(1)
                        ->where('starting_date', '<=', $curdate)
                        ->where('ending_date', '>=', $curdate)
                        ->first();

In addition, I need to validate this variable within an if statement where I can verify the timestamps, actions, and more..

Answer №1

Utilize Carbon Library

 $curdate = Carbon::now();
 $discount_db = Discount::whereActive(1)
                        ->where('starting_date', '<=', $curdate)
                        ->where('ending_date', '>=', $curdate)
                        ->first();
if(count($discount_db)){ 
    //perform an action here 
}else{
}

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

Incorporate a specialized module assistant within the template file of a separate module

After developing a new module connected to another database, I attempted to call the helper class from a different module template file named "description.pthml". The code I used is as follows: $_helper = $this->helper('ForumProdPosts/output' ...

What is the best way to transmit JSON data to PHP using AJAX?

I built a form to collect user information, which I then encode into JSON and send to PHP for storage in a MySQL database using AJAX. Below is the script I included just before the closing </body> tag. Unfortunately, I'm facing an issue where t ...

Symfony2 WebCaseTest experiences a failure with Absolute URL

Whenever I run the following code, it always shows an error message saying "No Route found", even though the Route does exist. $client = static::createClient(); $crawler = $client->request( 'GET', '/app_dev.php/admin/', a ...

How come this doesn't function properly in PHP: $a = explode(' ', $loadFunction)[0];

Is it possible to access an array without using another variable? For instance $loadFunction = "a b c d"; $a= explode(' ', $loadFunction)[0]; I am encountering a Parser error in PHP 5.3.3 Is there a workaround for this? ...

php code for locating a substring within a larger string

I need assistance with accessing a mysql database and extracting a specific segment of code from a column. The content in the column looks something like this: <image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5"> <file><![CDATA[image ...

Converting a PHP array into a jQuery array using <<<EOT syntax

I am facing an issue with my PHP code that outputs jQuery using the <<

The presentation of the Google graph with dynamically changing data appears to be displaying inaccurately

I am looking to incorporate a graph displaying sales and purchase data on my webpage. Users should be able to select from categories like Purchase, Sales, or Production. I have separate tables for Purchase (AccPurchase) and Sales (AccSales), with productio ...

Notify the user via email when they are on the WooCommerce checkout page and have items in their cart

Is there a way to trigger an email to be sent to the user when they are about to make a purchase at checkout? I tried implementing this code in my functions.php, but it seems to be sending multiple emails in a random manner. I suspect it may be due to the ...

Combining the values of two arrays into a single table using PHP

My task involves working with two arrays: $aGente = array('jan'=> 'm', 'alice'=> 'v', 'veronica'=> 'v', 'herman'=> 'm', 'maria'=> 'v', &apos ...

Is there a way to display (PHP for loop variables) within an HTML table using echo?

for ($q = 1 ; $q < 7 ; $q++ ) { echo $q ; echo $row; } While the above code functions properly, I am interested in echoing two rows as shown in the image below: https://i.stack.imgur.com/7KmUY.jpg I prefer not to use another for loop. Is there a way t ...

The data retrieved through ajax remains consistent every time

It seems like there might be a server-side cache causing issues on my server... Every time I make a request for content using jQuery AJAX, I receive the same data, even after replacing the server-side code with echo('hello, world!'); The confi ...

Tips for leveraging dynamic user-agent header with PHP Curl or Guzzle Client

I am brand new to PHP and I am currently exploring how to call a REST Service. To achieve this, I have the option of using either Curl or Guzzle Client in PHP. After setting it up, I tested calling the service from Mozilla and Chrome Browsers. However, I ...

Extracting POST information through PHP's AJAX Request

I am facing an issue where I keep receiving null values when using the following code: Here is my Ajax request: formData = { u: "3959eeadb32e02b85a792e21c", id: "6d7613df26" }; $.ajax({ ...

Analyzing user input in PHP against a mysqli database

I'm currently working on an online quiz application. I have everything in place with the form, and it's successfully sending emails to the administrator. However, there's a specific functionality that I want to implement: I need the script ...

Best method to conceal PHP errors and save them in the database

Trying to configure a PHP site for production, not development use. The goal is to hide all PHP errors from the user, but log them in a database table instead. However, when changing the defined constants to FALSE, both error display and logging fail: err ...

Error encountered while using the Braintree PHP SDK cURL functionality

Every time I attempt to generate a client token using Braintree_ClientToken::generate(), I keep receiving a Braintree_Exception_SSLCertificate exception. Despite having the SDK installed and meeting the minimum PHP requirements, I can't seem to figure ...

Utilizing jQuery and AJAX to submit a form

I'm currently facing an issue while attempting to transfer variables from an HTML form to a PHP script using jQuery and AJAX. Despite having successfully done this in the past, I am encountering difficulties this time around as PHP is failing to displ ...

Laravel responses with 404 for POST requests

Currently, I am in the process of creating a signup API that involves making two different API calls: first a GET call and then a POST call. When conducting the GET [POSTMAN] call, the response from the controller is correct but upon making the POST call, ...

Having trouble rendering JSON encoded data in a JqPlot Chart within a PHP script

I've spent the past few days scouring through Stack Overflow and various other websites, but I haven't been able to find a solution to my specific issue. Even the book 'Create Web Charts with JqPlot' by Fabio Nelli didn't provide t ...

Error in NPM when trying to run the command npm run watch on parameter 1 for scss

I encountered an error while working on webpack.mix.js file. The error message indicates that I am missing a required parameter 1 in the scss section of my code: > @ watch /media/ricardo/FILES/projects/Laravel/costurita-etl > npm run development -- ...