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()
Related
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();
I'm trying to group my meetings by date so I can get the meetings from today and the next three workdays. But that's not working very well. I'm always getting the next days AND the last day but I don't want that.
$grouped_meetings = Meeting::whereBetween('date', [Carbon::yesterday()->toDate(), now()->addDays($days_to_add)])->orderBy('date')->get()->groupBy(function ($val){
return Carbon::parse($val->date)->format('l');
});
I thought it works like selecting the dates between yesterday and the day I specify after that in my code. But it's always returning the meetings from yesterday too.
I tried changing the Carbon::yesterday()->toDate() to Carbon::now()->toDate() and even now() but then I miss the current day.
I have absolutely no clue why this is working like that and I don't know how to solve this problem.
Edit: the value of date is always a value in the format of YYYY-MM-DD.
If you use carbon now, it will include the time. So for example 2019-07-24 12:34:26
if you use carbon yesterday it will be yesterday at 00:00. So for example 2019-07-23 00:00:00
So you need to use Carbon now and set the timestamp to the beginning of the day, i.e. 00:00:00.
Or another way is you could use carbon yesterday and add a day
(Posted solution on behalf of the question author).
$grouped_meetings = Meeting::whereBetween('date', [Carbon::now()->startOfDay()->toDate(), now()->addDays($days_to_add)])->orderBy('date')->get()->groupBy(function ($val){
return Carbon::parse($val->date)->format('l');
});
The startOfDay() in combination with Carbon::now() fixed the problem.
Hello I'm trying to check how long message is in database I'm using laravel4 framework and I have this:
date( "h", strtotime($message->created_at)) - date('h')
but it only counts hours I need to change days and month to hours and then count how long is it in the database. How can i do it?
date() returns a string, and you can't do math on strings. date('h') returns the current hour, eg: 10 now because it is 10am. If $message->created_at was Feb 3, 1978 10:01:02 then date('h', strtotime($message->created_at)) would return 10 as well.
Assuming $message->created_at is in an acceptable format:
$diff_in_hours = (time() - strtotime($message->created_at)) / 3600;
Your question is a bit unclear, but it sounds to me like you're looking for what is called a "human readable timestamp". If you've set up your database table correctly, both created_at and updated_at should be retrieved as carbon objects, giving you several different ways to display your date. It's actually very simple to achieve the desired effect:
$message->created_at->diffForHumans()
This should then print a readable time just like you see here on stackoverflow (1 hour ago, 23 minutes ago, etc...).
Check out the link for more information about carbon objects.
I've already seen some answers in StackOverflow about how to calculate difference in time between two dates. But no answer are using the DateTime obejct or the Interval Object in PHP. I got the following code snippet from PHP Manual website: http://www.php.net/manual/en/dateinterval.format.php.
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
But the problem is that total days equals 6015 days when it should only be 31 days. I tried to access the instance variable days in the Interval object. It too shows 6015 days. But the instances for month and days intervals are correct. Can someone tell me why?
And I want to use these objects to calculate the difference in times!
Many thanks
UPDATE:
I think it was just a problem with my PHP setup
Running your code...
31 total days
1 month, 0 days
PHP setup issue, perhaps?
I ran that exact same script and got "31 total days" and "1 month, 0 days" (the expected values). Try upgrading your php maybe?..
I'm not sure what version of PHP you're on, but in my experience, I've made it a habit to always set my timezone with date_default_timezone_set('America/Los_Angeles'); where America/Los_Angeles is the timezone you want to be using. I don't know how this ultimately affects the date calculations, but I do know that PHP 5.3 will yell at you if you try and do date manipulation without specifying a default timezone.
I believe you can also set a default timezone in your php.ini- which I think by default is set to UTC.
I have publish up date and publish down date in my DB.
Currently they are both same dates.
How do I change it (during mysql insert) so publish down date is 30 days past publish up date.
I am using $pubDate
Thanks
You can use DATE_ADD():
DATE_ADD(my_date, INTERVAL 30 DAY)
in php, before inserting you can use strtotime():
if the publishDown date is a timestamp:
$publishDown = strtotime("+30 days",$publishDown);
otherwise you may have to use mktime to get it in the right format