how to compare today to another date with where - php

I need to compare today to the date twenty business days after from created_at. I have afterTwentyBusinessDays function. Is it possiple to use it with where in laravel?
->where(afterTwentyBusinessDays($created_at), today())

You have to get a date that was twenty days ago and then set in where condition
->whereDate('created_at', date('Y-m-d', strtotime('-20 days')));

I presume afterTwentyBusinessDays($created_at) return carbon date time object.
Try $query->whereDate('created_at', '>', afterTwentyBusinessDays($created_at))

The most efficient way is to use carbon
now()->diffInDays('2023-1-23 07:06:31') // 19
now() will give you carbon instance alongwith UTC date and time. You can compare any dates, years, months, months, weeks even seconds.
Explore Carbon docs or visit detailed Carbon methods and their uses
https://www.digitalocean.com/community/tutorials/easier-datetime-in-laravel-and-php-with-carbon
Why you are not using Eloquent Model's own predefined methods to compare date and times like whereDate(), whereDay(), whereMonth() or wheryear() there are many.
Also you can play with these days, remember to parse in your timezone and format
User::whereDay('created_at', '>', '2023-1-23 07:06:31')->get();

Related

Show yestearday and old 30 days instead date laravel

is there a way to display in bales instead of yesterday or older than 30 days? forget how I did it for yesterday but it doesn't work.
#elseif (Carbon\Carbon::parse($ns->created_at)->toDateString() === date('Y-m-d', strtotime('-1 day')))
It should have been displayed to me yesterday for this condition but it does not display. And for 30 days I would do the same
You can achieve this via diffForHumans function provide by Carbon.
Since the created_at field is a carbon instance, you can achieve this by doing
$ns->created_at->diffForHumans()

How to Substract time from datetime

I am using a timezone based script where there is deadline based on timezones. Say for example a deadline is on 7th July at 6PM for a person in IST timezone. The deadline should be 1:30 less than 6pm in Dubai as per their timezone.
I have already calculated the difference between the two timezone difference. I am stuck at deducting that calculated time from the deadline time.
I have saved the timezones in +5:40 +4:00 -4:00 this format instead of using php default ones.
Here's what I use to add and subtract time from a date.
First of all, I get the date from the database and then convert it to a DateTime object.
$date = new DateTime($date);
I use add and a DateInterval to add time. Here's an example to add hours.
$date->add(new DateInterval("PT{$hoursToAdd}H"));
And here's and example to subtract hours intead:
$date->sub(new DateInterval("PT{$hoursToSubstract}H"));
Check this out to know how to work with DateInterval and add/subtract different times: http://php.net/manual/en/class.dateinterval.php
Can you explain a little more specifically what you want to do?
You want to subtract when data already on some var inside your script or you want the SQL query needed ?
If so maybe you can use this answer ---> How to subtract 3 hours from a datetime in MySQL?
The last answer of this post maybe is the one that fit the most what you need :
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT
DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'), '%Y-%m-%d') AS date
FROM
x
ORDER BY
date ASC;

Finding the difference between days

I'm having a table as follows to store future date,
email date
abc#gmail.com 8/10/2014
and I want to do is to find the difference between the above date and the sever date.
I'm using date("m/d/Y") to get the current date.
If date("m/d/Y") = 07/20/2014, then I need the answer as 21.
Please help me find the difference between those days using PHP & MySQL, or suggest a better way to find the difference in days.
You can convert date to timestamp then calculate the days:
(int)(strtotime('8/10/2014')-strtotime('07/20/2014'))/60/60/24
The easiest way would be to store your dates as timestamps, then you could subtract the current timestamp with the one you've saved in a database.
The PHP function time() returns the current timestamp – that is the number of seconds since the 1st of January 1970. You can then format a timestamp $stamp to your liking with date('m/d/Y', $stamp), for example.
Aside from facilitating arithmetic operations with dates, you can display more or less information with timestamps by formatting them with date(), as well as show different formats (July 13, 2014 vs. 07/13/2014). If you save a date as a string, e.g. "8/10/2014", it will be very complicated for you to change the format, and it won't be possible to get the correct time, for example.
Finding how long ago in days a timestamp $stamp was to the current time is very easy:
$now = time();
$days = ($stamp - $now) / (24*3600);
Use round() to get a full number if desired (e.g. 7.2309 would simply become 7).
I'd personally do it using DateTime:
Select your date out into a variable. In this instance We'll call it $DBDate
$DBDateObj = DateTime::createFromFormat('j/m/Y', $DBDate);
$TodayDateObj = new DateTime();
$interval = $TodayDateObj->diff($DBDateObj);
$daysDiff = $interval->days;
In $daysDiff you should now have the difference in days.

Operations with dates in cakephp

How do operations with dates in cakephp?
I want to calculate the difference in days between two dates, for example: a date subtracting 10 days or a date and add 20 days.
How I do that?
You can use this:
CakeTime::format('r', '+20 days', true);
Take a look on this: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
Maybe you wan't see this one too: http://www.php.net/manual/en/function.date.phpenter

Unix timestamp before 1970 (even before 1700), using PHP

how can i work with dates before 01.01.1970?
i need to get first day of the month for dates from year 1700.
how can i achieve that?
thank you in advance!
You can use DateTime.
Example:
<?php
$d = new DateTime("1780-06-01");
echo $d->format("l"); //Thursday
You should, however, consider that the Gregorian Calendar was adopted in different instants throughout the world.
Try using the Zend Date classes, or the older PEAR Date class
If the year is <1970 or the value is negative, the relationship is undefined.
-- The Open Group, Single Unix Specification, Base definitions (4: General concepts)
As you can see, it's not defined how Unix timestamps before 1970 would map to dates. It is only defined for later dates.
If you want to go to dates before 1970, you need to always work with actual dates, like ISO dates (2008-12-31T23:59:59). You can use DateTime class to represent any date you can think of but ever created it from a Unix timestamp or convert it to a Unix timestamp if the year is before 1970 as that will simply yield undefined results.

Categories