Get Next Day date with fix time in PHP - php

Hello i am writing a module in PHP. Here I have to check for the condition like if user select demo for application with basic features he should get demo for next day till 12 PM. I am using code
date_default_timezone_set("Asia/Kolkata");
echo date("Y-m-d 12:00:00",strtotime('tomorrow'))
it is working fine. But when user try for demo account in mid night that is same date as next day. it is giving me the day after tomorrow date. For example today is 22/01/2014 before 22/01/2014 11:59PM it is working fine. but 23/01/2014 00:01 AM it is giving date 24/01/2014 12:01PM but i want it to be 23/01/2014 12:01PM. Any suggestion.

date_default_timezone_set("Asia/Kolkata");
$day = date('H') < 12 ? 'today' : 'tomorrow';
echo date("Y-m-d 12:00:00",strtotime($day));
The key is to work out which day the demo should end, this can be done by checking the current hour. If the current hour is less than 12, then it should be today's date, otherwise tomorrow's.

Related

Laravel get total time workerd by employee between two dates using carbonInterval or carbon php

i have time shifts which are assigned to the user. Suppose a night shift starting time is 21-00-00 pm of one july and its ending time is 03-00-00 am of 2nd July. Now i want to get total time a employee worked by adding start time to end time which is equal to 6 hours and i should get six hours. I have tried following code which is working fine for current date like it will give me exact 6 hours if start time is equal to 15-00-00 pm of 1 july to 21-00-00 pm of 1 july but it will fail when shifts exists between two dates as i mentioned above.
$attendance_start_time = \Carbon\Carbon::parse($shift->start_time);
$attendance_end_time = \Carbon\Carbon::parse($shift->end_time);
$total_attendance_time=$attendance_end_time->diffInSeconds($attendance_start_time,true);
\Carbon\CarbonInterval::seconds($total_attendance_time)->cascade()->forHumans()
i am expecting six hours but it is giving me following result
18 hours
i want exact six hours
Not sure if it will fully solve your problem, but check out this :
\Carbon\CarbonInterval::seconds(4100)->cascade()->forHumans(['aUnit' => true]);
UPD:
It might be this solution will work in your case, but make sure that you have tested all of the edge-cases:
$startTime = \Carbon\Carbon::parse('2022-07-02 19:00');
$endTime = \Carbon\Carbon::parse('2022-07-02 19:30');
$diff = $startTime->diffInSeconds($endTime);
if ($endTime->greaterThanOrEqualTo($endTime) && ! $endTime->isSameDay($startTime)) {
$diff = $startTime->diffInSeconds($endTime->addDay());
}
$humanReadable = \Carbon\CarbonInterval::seconds($diff)->cascade()->forHumans(['aUnit' => true]);

is there any way to keep the month same?

I have a condition in laravel where I have to put a min date on html input type date but the condition is complex.
Condition :
1 : User should only be able to select date from past ten days.
2 : Only two days from previous month. Example lets today date is 4 april then the user should
only be able to select from 4 april to down 1 april and 2 days previous month.
so I can put the min="some date" in input of date
You didn't specify how you actually set the date, but here is a PHP version that should get you closer. All you need to do is extract the parts you need from $minDate.
$lastTwo = new DateTime(date('Y-m-d', strtotime(date('Y-m-1') . ' -2 days')));
$lastTen = new DateTime(date('Y-m-d', strtotime('-10 days')));
$minDate = max($lastTen, $lastTwo);
Use Carbon::now()->month; to get current month and make conditon according to your query.

Get day of the next week by monday with php

i want to get the day of the next week with php..i need something like this
If today is monday, i want to display the days (es 18-19-20) of the next week, not of this week..the week start on monday...but it will work also if today is sunday..always the days of the next week..i can't found anything because i tried to use jddayofweek but i don't understand it maybe..i haven't found an italian guide..
I have a table which display the days in stringn form..so sunday monday etc, fir my problem i tried date() function but if i use +1 on the day i will have problem with february for example
Not sure exactly what you're doing, but maybe something like:
echo date("d", strtotime("monday next week"))
This will give you next Monday:
$date = date_create()->modify('monday next week');
echo date_format($date, "d");

Counting database for previous month across the new year roll over PHP SQL

I have had a custom wordpress plugin that runs a report for the database entries from the previous few months, and it worked all year, but now that the new year has happened, it shows no results because it is counting back a few months but then keeping it for 2016.
For example, I load the page on Jan 1st 2016, and it shows the results for December 2016 instead of 2015 when I am using the following method for getting last month's data.
$today = date("Y-m-d H:i:s");
$month_current_start = date("Y-m-")."1 0:0:0";
$currentmonth = date("m");
$lastmonthnum = $currentmonth - 1;
$last_month_start = date("Y-").$lastmonthnum."-1 0:0:0";
$last_month_end = date("Y-").$lastmonthnum."-31 0:0:0";
so then I have an SQL that says something like
$var = $wpdb->get_var( "SELECT Count(*) FROM `table` WHERE table.timestamp BETWEEN '$last_month_start' AND '$last_month_end' AND table.amount = 1200" );
Any tips on how to fix this query so that it knows that last month is 2015?
I'm no professional so I'd love beginner-style help :-)
I can make it work manually but i'd like the code to work for the next few months automatically (since the actual plugin calculates the last four months).
Easiest way would be to convert last month to a time stamp, and use that for creating the format you have in your DB. An example based on what you have so far would be:
$today = date("Y-m-d H:i:s");
$month_current_start = date("Y-m-")."1 0:0:0";
$ltime = strtotime("-1 month");
$last_month_start = date("Y-m",$ltime)."-1 0:0:0";
$last_month_end = date("Y-m",$ltime)."-31 0:0:0";
What this does, is it makes a timestamp for 1 month ago from when it is called in the line $ltime = strtotime("-1 month"); From there, it makes the format for the first day and last day of the month (assuming there are always 31 days of course ^^) based on the timestamp, which you'll use to provide the year and the month.
PHP Sandbox with the code example and it's output if you'd want to play around with how strtotime works.
In mysql you have the option to use year_month, see this for reference.
Get the current year and month using your code
update your query accordingly
or you can have your sql server do all the work for you by using the date_add function
Update your question with the db server you are using, then I can write the new query for you.

php date/time intervals with subscription

I need some help wrapping my head around this. I am trying to develop something that does monthly, weekly, quarterly, yearly subscriptions. I need to figure out when the next charge date is. But I'm getting confused getting through the logic. I can easily calculate when a year is from the original transaction (<? echo date("Y-m-d",strtotime($charge_interval, strtotime($original_charge_time))); ?>), where $charge_interval is '+3 months' or '+1 month', etc. But.. if 3 months have passed, I can't just use that equation, because it will show the "next" date as one month from the original. Any advice?
EDIT: more precision. My data is stored in a DB, and I need to ensure its getting the "next" month rather than a month from original date. Say transaction took place Nov 15th. Its now July 6th. I want "next transaction" to say July 15th, NOT Dec 15th (which is what the above code would accomplish), and to say "we have already have 7 charges before".
strtotime is little-know, but can show itself to be very useful for this kind of use.
EDIT: here may be a clue:
$charges = 0;
$now = time();
$next_charge_time = strtotime($original_charge_time);
while ($next_charge_time < $now)
{
$charges++;
$next_charge_time = strtotime($charge_interval, $next_charge_time);
}
$next_charge_time = date("m/d/Y", $next_charge_time);
echo "You have been charged $charges times since $original_charge_time.";
echo "<br/>";
echo "Next charge will be on $next_charge_time";
Doesn't php have a dateadd function of some kind to add a number of months to a date?
Like this one?
http://php.net/manual/en/function.date-add.php
Use DateTime() with http://us3.php.net/manual/en/datetime.add.php
Try mktime(0, 0, 0, strftime("%d", $origtime), date("m")+1, date("Y"));
that would mean midnight of course (first three parms are H, M, S, you could use strftime for those too if you want)
that would work for the next calendar month, you could just remove the "+1" for the same month if you work out the day falls later than the current day.

Categories