Carbon dates find out if today is thursday or go back - php

I have the below code running in one of my scripts and it works well, but feels rather clunky and long. I feel like there might be a much shorter way of achieving the same result. I also mean without using a shorthand if statement.
I need to find out if today is thursday and if not use the previous thursday as the date. Any thoughts / ideas would be great.
<?php
if (new Carbon('this thursday') > new Carbon()) {
$date = Carbon('this thursday');
} else {
$date = Carbon('last thursday');
}
?>

According to
http://carbon.nesbot.com/docs/#api-modifiers :
The $date will hold the date to be shown.
<?php
$today = new Carbon();
if($today->dayOfWeek == Carbon::THURSDAY)
$date = $today;
else
$date = new Carbon('last thursday');
?>

Related

How to get difference between two day in php

I have on function which passing some parameter the like
everyWeekOn("Mon",11,19,00)
I want to compute the difference between the current day (e.g. 'Fri')
and passed parameter day i.e. Mon.
The output should be:
The difference between Mon and Fri is 3
I tried it like this
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But know I don't know what to do next to get the difference between the two days.
Note that this question is different from How to calculate the difference between two dates using PHP? because I only have a day and not a complete date.
Just implement DateTime class in conjunction with ->diff method:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
You can find no. of days in two days by using this code
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
Please use this may this help you

PHP: DateTime '-1 day'

I want to get the date of yesterday of the current date in a specifc time zone.
I tried like this, but it is not working:
$date = new DateTime(NULL, new DateTimeZone('Pacific/Wake'));
$yesterday = $date->modify( '-1 day' );
$yesterday = $yesterday->format('Y-m-d');
I am still getting today's date.
This problem, according to the documentation for the modify() method, seems to entirely depend on which version of php you're using. In this case, method chaining(which is what you're attempting to do is called), is only available on php version 5.3.0 and up, according to the changelog on the previously linked docs.
That in mind, it explains why your code didn't work, and #Deryck's did. If you ever do upgrade your php version, or get your host to upgrade it, you could likely reduce those three lines to two:
$date = new DateTime(NULL, new DateTimeZone('Pacific/Wake'));
$date = $date->modify( '-1 day' )->format('Y-m-d');
Not much of an improvement, I realize, but there's your reason for why it failed to work.
Below are two of the methods I see of getting around this; one is creation of a class.. which seems like overkill to me unless this is apart of something grander... the other is a creation of a function. Both shove the extra lines into something that takes up less space, in a sense.
class DT {
private $now; //can be null
private $timezone; //DateTimeZone object
public function __construct($tz_str, $now = NULL) {
$this->now = $now;
$this->timezone = new DateTimeZone($tz_str);;
}
public function mod($mod,$format) {
$dt = new DateTime($this->now, $this->timezone);
$dt->modify($mod);
return $dt->format($format);
}
}
function mod_formatted_zone($tz_str, $mod, $format, $now = NULL) {
$timezone = new DateTimeZone($tz_str);
$dt = new DateTime($now,$timezone);
$dt->modify($mod);
return $dt->format($format);
}
The use of either is simple; in the case of the class, it'd be something like..
$dt = new DT('Pacific/Wake');
echo $dt->mod('-1 day', 'Y-m-d');
While in the case of the function, it'd simply be..
echo mod_formatted_zone('Pacific/Wake', '-1 day', 'Y-m-d');
Seems to work once you don't re-assign the $date variable unnecessarily. See below:
<?php
$date = new DateTime(NULL, new DateTimeZone('Pacific/Wake'));
$date->modify("-1 day");
$date = $date->format("Y-m-d");
// echo $date; // just in case you wanna echo - ya dig
?>
View demo
FYI:
Wake Island Time Zone (UTC+12:00)
Which means 1 day before is actually today (for me at least, on the western hemisphere).
I want to get the date of yesterday of the current date in a specifc time zone.
You can specify relative dates in the DateTime constructor. This will work for you:-
$yesterday = new DateTime('- 1 day', new DateTimeZone('Pacific/Wake'));
var_dump($yesterday);
Proof!.

PHp calculate Time Difference (ignore dates) from unix time

What's the simplest way to do this?
$known_time = '19:33:39' //GMT
I want to get the current GMT Time and calculate if it is past $known_time
I do not care about the dates, I simply want to know if the time of day right now in GMT is later than $known_time
My attempts so far were clumsy as I first checked the hour...than the minute. I'm sure there's a more elegant method. Thank you!
I don't know what elegant is for you but alternatively, you could just also DateTime objects:
$now = new DateTime('GMT');
$known_time = new DateTime('19:33:39', new DateTimeZone('GMT'));
if($now >= $known_time) {
echo 'now is greater';
} else {
echo 'now is less than the time you provided';
}
you can use PHP 5.2 or above and the following functions are available there:
$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h:%i:%s');
or
what you can do is use: strtotime($yourtime)-strtotime(othertime), this will give you the difference.and then use round() to basically round the numbers..

Why is my while loop not working when no echo is put inside?

I got a pretty simple code that will take 2 dates and loop my data until the end date is reached.
$start = new DateTime($senddate);
$now = new DateTime("NOW");
$end = new DateTime ($end);
//We check if starting date is >= now()
if ($start->date <= $now->date){
$start = $now;
}
$i=0;
if ($frequency==4){
while ($start->date <= $end->date) {
$calcdate[$i]=$start->date;
$start->modify('+1 month');
$i++;
echo '<!--';
print_r($start);
echo '-->';
}
As you see there is a print_r inside the loop.
Everything work fine :)
BUT, if I remove it, then the loop never end .. I tried to add if($i>50) exit; without anymore success. I don't understand why this loop doesn't work when no pint_r is inside.
Thanks for your help
I would suggest that you have a read of the PHP DateTime manual, there is a lot of good information there that will help you with what you are trying to do.
As far as I can tell, you are trying to carry out an operation on a monthly basis between two dates that span the current date. There is a simpler way of doing it utilising the DatePeriod class.
Something like this:-
$start = new \DateTime('yesterday');// Just for demo purposes
$now = new \DateTime(); //No need for "now" as it is the default
$end = new \DateTime('+ 6 month');// again, for demo purposes
$interval = new \DateInterval('P1M');
if($now >= $start){ // you can do direct comparisons on DateTime objects
$period = new \DatePeriod($start, $interval, $end);
foreach($period as $date){
// Each $date is a DateTime instance that you can
// operate on as you see fit, I have just var_dumped it out.
var_dump($date);
}
}
The above code can be seen working here http://3v4l.org/1I30E
My error came from this line $calcdate[$i]=$start->date;
It seems that this gives somme unexpected behavior (in this case), I tried using $calcdate[]=$start->format('Y-m-d H:i:s'); which gave the expected results ... Don't know why my script didn't worked with the date method. If anyone knows ..

Calculate date php issue with strtotime

Still trying to figure out date with PHP. I have this:
$fri_end_date = date('Y-m-d',strtotime('Friday'));
$fri_start_date = date('Y-m-d ',strtotime('Friday'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
I want to calculate the date for each Friday but display that Friday date through till Sunday. Then on Monday it would show the next coming Friday. Basically want to show the same Friday date throughout the weekend. How would I do this?
I did understand your question now and I think this will solve your issue:
if( date('w') >= 6 )
$fri_end_date = date('Y-m-d',strtotime('last Friday'));
else
$fri_start_date = date('Y-m-d ',strtotime('Friday'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
Using date('w') will return you the day of the week, that way you can test if it is saturday or sunday and use the 'last Friday' or else use the "current" Friday.
Cheers,
Denis
Try this and let me know if it is what you want:
$fri_end_date = date('Y-m-d',strtotime('last Friday'));
$fri_start_date = date('Y-m-d ',strtotime('next Friday'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
This will display the date of the last Friday before the day you run the code and the next Friday.
Cheers,
Denis
The problem is that you are not really clear about your problem. That sayd, if I understand correctly you need to display 2 days before and after the current Friday. If that is the case try the code below.
$fri_end_date = date('Y-m-d',strtotime('Friday -2 days'));
$fri_start_date = date('Y-m-d ',strtotime('Friday +2days'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
If it is not, try to be more concise and maybe throw in an example or two ;)
Cheers,
Denis

Categories