I have job_no with other fields in my database, I need to identify the date and according to that date job no should be generated automatically. The date criteria is financial year April to March.
If financial year change, job no should start from 1 else continue.
What can I do in this situation?
I'm using php and mysql.
Thanks.
Make a function in php that check for the date and month so that you get to know which financial year a date lies. In that function you can check the financial year of current date and according to that either continue with the last incremented value, or restart the value from 1.
The function definition is something like:
if (date('m') > 3) {
$year = date('Y') +1;
}
else {
$year = $date('Y');
}
or
if (date('m') > 3) {
$year = date('Y')."-".(date('Y') +1);
}
else {
$year = (date('Y')-1)."-".date('Y');
}
Related
I want to find entries in a month range based on date of a month
like if a user registered on 20th of a month, the script should get entries in a range from last 20th to next 20th of the month.
I.e if the script is running on any day before 20th of April the range should be March 20 - April 20, and if its running on 20th April or after then the range should be April 20 - May 20.
I looked up relative formats but it only lists functions for day names and weeks etc.
Is there any way the relative date format works like last n to next n. where n= 1 to 31.
Can anyone help? Thanks
Based on comment from Cully, here is an implementation (it still feels too messy, maybe there is an easier way to do it). It may explain the question a bit more.
function getFromDate($myDate, $nDate)
{
// sub 1 day till date is $nDate
while(true)
{
if($myDate->format('d')==$nDate)
break;
$myDate->sub(new DateInterval('P1D'));
}
return $myDate;
}
function getToDate($myDate, $nDate)
{
// add 1 day till date is $nDate
while(true)
{
$myDate->add(new DateInterval('P1D'));
if($myDate->format('d')==$nDate)
break;
}
return $myDate;
}
$timestamp = 1602107066; // An example user registration date, 7 October 2021
$nDate = gmdate("d", $timestamp);
$fromDate = getFromDate(new DateTime('now'), $nDate);
$toDate = getToDate(new DateTime('now'), $nDate);
echo $fromDate->format('d M y')."\r\n"; // 7 May 2021 (run on May 22 2021)
echo $toDate->format('d M y'); // 7 June 2021 (run on May 22 2021)
Do you mean something like this? It might not be exactly what you want, but can you use it to create what you want?
<?php
$numberOfDaysIWant = 20;
// March 20th, 2021, but you can use any date you want
$startDate = new DateTimeImmutable('2021-03-20');
$myPastDates = [];
$myFutureDates = [];
$currentDate = $startDate;
for($i = 0; $i < $numberOfDaysIWant; $i++) {
$currentDate = $currentDate->sub('P1D');
$myPastDates []= $currentDate;
}
$currentDate = $startDate;
for($i = 0; $i < $numberOfDaysIWant; $i++) {
$currentDate = $currentDate->add('P1D');
$myFutureDates []= $currentDate;
}
var_dump($myPastDates, $myFutureDates);
It's unclear from your question, but it sounds like maybe you want to get the $numberOfDaysIWant value based on the date of the selected month. If so, you could use this to get it:
<?php
$startDate = new DateTimeImmutable('2021-03-20');
$numberOfDaysIWant = (int) $startDate->format('j'); // "j" gives you the day of the month, without leading zeroes
Having a php challenge with dynamically setting a day of the week say -thursday, to a function and have the function loop through each day (friday, saturday.. tuesday until a condition i>0 is met.
I have searched thoroughly but could not finding a matching solution.
Please see break down below:
My form:
showing user input that's counted
My code:
//get duraiton in days
function durationInDays($data) {
$startDate = date_create($data['startdate']);
$endDate = date_create($data['enddate']);
$dateDiff = date_diff($startDate,$endDate);
$durationInDays = $dateDiff -> format("%a");
return $durationInDays;
}
//pass duration in days to function to break into weeks and days
function numberOfWeeksAndDays($durationInDays) {
$days = $numdays%7;
$weeks = floor($numdays/7);
return [$weeks, $days];
}
//parse in number of days into function to loop starting from
//day of week of start date say --thursday for example with a
//condition to stop loop once numberOfDays is zero.
function extraMassCount($dayOfWeek, $numberOfDays, $countMass) {
$daysOfTheWeek = [
'monday','tuesday','wednesday','thursday','friday','saturday','sunday',
'monday','tuesday','wednesday','thursday','friday','saturday','sunday'
];
$massCountForExtraDays = 0;
for ($i = $numberOfDays; $i > 0; $i--) {
if($daysOfTheWeek[$i]=='monday')
$massCountForExtraDays += $countMass[0];
if($daysOfTheWeek[$i]=='tuesday')
$massCountForExtraDays += $countMass[1];
if($daysOfTheWeek[$i]=='wednesday')
$massCountForExtraDays += $countMass[2];
if($daysOfTheWeek[$i]=='thursday')
$massCountForExtraDays += $countMass[3];
if($daysOfTheWeek[$i]=='friday')
$massCountForExtraDays += $countMass[4];
if($daysOfTheWeek[$i]=='saturday')
$massCountForExtraDays += $countMass[5];
if($daysOfTheWeek[$i]=='sunday')
$massCountForExtraDays += $countMass[6];
}
return $massCountForExtraDays;
}
The problem: If $numberOfDays=6,then i=6 and the function starts with friday which is not the start date.
My question: how do I implement dayOfWeek parameter so the function extraMassCount will start counting dynamically e.g thursday if startdate is thursday and not the way it is hardcoded to start? I hope my question is clear.
That is, as shown in the form, the function is supposed count the number of masses checked per day and add them together. Starting from startdate to the enddate. Once the durationInDays is broken down to weeks and days I need the function to start at the startdate say --wednesday and add countMass(which is a count of the Masses checked by the user with datatype int) for each day onward.. thursday, friday, etc. – I appreciate the help!
Like this, with relative date formats and DateTime class
$Date = new DateTime;
//go to monday
$Date->modify('monday this week');
for($i=0;$i<10;++$i){
echo $Date->format('l')."\n";
$Date->modify('+1 days'); //go to next day
}
Output
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
Tuesday
Wednesday
Sandbox
I think the rest you can work out as I am not really sure what that does.
If you want a number for the day, for like this part $countMass[0]; you can use the w format. But 0 is Sunday, if I remember correctly.
for($i=0;$i<10;++$i){
$Date->modify('+1 days');
//dont assume your inputs will be correct
if(isset($countMass[$Date->format('w')])){
$massCountForExtraDays += $countMass[$Date->format('w')];
}
}
PS. instead of all these separate if statements, which is bad performance wise. I would just use a switch statement, but at the very least connect those with else so they don't all evaluate every time, needlessly. $daysOfTheWeek[$i] can only equal one of those. If you notice above, I just optimized them out.
I am prepping our businesses website for the Christmas period between November 1st until December 31st at midnight. I have already made the amendments on the sites CSS style-sheet to trigger the Christmas theme overwrite when the class of "xmas" is added to the body element if have placed the code I have for this into our global head.php file but it's triggering just now and I am trying to catch this within PHP tags to trigger with the date function?
Is this possible - If so could someone help?
So far I have tried this...
<?php
//Must be in format of DAY-MONTH-YEAR
if(date('d-j-Y') == "30-11-date('Y')") {
?>
<script>
$(document).ready(function(){
$('body').addClass('xmas');
});
</script>
<?php
}
?>
But of course this will only work on the 30th of November and I need this to work though until the 31st of December.
Use DateTime objects are they are comparable and make this fairly easy and straightforward.
<?php
$now = new DateTime();
$nov1 = new DateTime('2015-11-01 00:00:00');
$dec31 = new DateTime('2015-12-31 23:59:59');
if($now >= $nov1 && $now <= $dec31) {
?>
If you want to make the code dynamic each year just make the year value dynamic as you have in your example.
To have a script that whatever is the year or date, it always tell you if its time of xmas and new year eve holidays
i have use it in PHP
/*
* ==12 if is decembre, >= 17 if day is great than 17
* ==12 if is january, <= 7 if day is less than 7
* #return if is holiday time or not
*/
public static function isHolidayTime()
{
if(date("m") == 12 && date("d") >= 17){
//Holiday time there
return true;
}else if(date("m") == 1 && date("d") <= 7){
//Holiday time there
return true;
}
else{
//normal day
return false;
}
}
You can use the date function with month and day, like so:
$date = date('md');
if($date >= '1130' && $date <= '1231')
CONTEXT: My client, a local movie theater, runs a Sunday Matinee Special every other Sunday starting with the SECOND SUNDAY every year. So for this year the dates are 1/11, 1/18, 2/8, 2/22, .... [The only exception is the SUNDAY after their film festival which runs the the THIRD FULL WEEK OF OCTOBER, but automating this single exception is a "would-be-nice" item, not essential.]
MY SKILL LEVEL: BEGINNER (I need your help!) I believe I need to use a combination of mktime() and date() but I don't know how to put it together.
WHAT I'VE TRIED: I suspect the answer is a combination of what I see on these three posts:
(1) a do-while loop to get a specific day of the week from a date range
(2) there may be a shortcut for referencing the second sunday in the ACCEPTED ANSWER here, but I'm not sure this helps
(3) MOST RELEVANT(?): Get the First Sunday of Every Month
END RESULT: I want to display the [Month] and [Day] of the next Sunday Matinee (so I want to find and display the first item in the array AFTER the current date). The text will appear like this: "Next: [Month] [Day]"
Make sense? Let me know if I've forgotten anything.
If it's not too much to ask, please explain your code so I (and others?) can learn from this; but I'd be more than grateful for "merely" a straight-up solution.
Many thanks.
Debra
UPDATE/PROGRESS: This code will get me the array of Sundays:
$startDate = strtotime("second Sunday of ".date('Y')."");
for ($i=0; $i < 27; $i++){
$sundays = date('F j', ($startDate + (($i*14) * 24 * 3600))) . '<br>';
print $sundays;
}
NEXT TO FIGURE OUT: write a statement to find in the array of Sundays the first date after the current date.
This is a pretty manual, procedural solution, but it should work.
<?php
$SECS_PER_DAY = 86400;
# Find the first Sunday on or after a given timestamp
function firstSundayOnOrAfter($time) {
global $SECS_PER_DAY;
# What day of the week is the given date?
$wday = date('w', $time);
if ($wday == 0) {
# it's already a Sunday
return $time;
}
return $time + (7 - $wday) * $SECS_PER_DAY;
}
# return an array of timestamps representing
# (noon on) the special matinee Sundays for the given year
function specialMatineeSundays($year) {
global $SECS_PER_DAY;
# When's the film festival?
$oct1 = mktime(12,0,0,10,1,$year);
$festivalStart = firstSundayOnOrAfter($oct1);
$festivalSkip = $festivalStart + 7 * $SECS_PER_DAY;
# find the first Sunday of the year
$jan1 = mktime(12,0,0,1,1,$year);
$sunday = firstSundayOnOrAfter($jan1);
# Add a week to start with the second Sunday
$sunday += 7 * $SECS_PER_DAY;
# Build up our result list
$result = [];
# As long as the Sunday under examination is still the same year,
# add it to the list (unless it's the post-festival skip date)
# and then add two weeks
while (date('Y',$sunday) == $year) {
if ($sunday != $festivalSkip) {
$result[] = $sunday;
}
$sunday += 14 * $SECS_PER_DAY;
}
return $result;
}
I am trying to get stripe to set a end_trial date on the next occurrence of whatever day of the month the user chooses. i.e. If today is the 16th and the user chooses the 15th I need the unix timestamp for the 15th of the next month. However if today was the 14th I need the timestamp for tomorrow.
I tried the solution found on this SO question Find the date for next 15th using php .
When i ran the code suggested in that question and substituted 15 for 31
$nextnth = mktime(0, 0, 0, date('n') + (date('j') >= 31), 31);
echo date('Y-m-d', $nextnth);
The result is 2013-03-03
I also tried this one Get the date of the next occurrence of the 18th .
The second one would actually give me 2013-03-31 when i ran it one 2013-1-31.
Both had unexpected results. Is february the problem? Any guidance will be much appreciated.
Here is a way to do it.
function nextDate($userDay){
$today = date('d'); // today
$target = date('Y-m-'.$userDay); // target day
if($today <= $userDay){
$return = strtotime($target);
}
else{
$thisMonth = date('m') + 1;
$thisYear = date('Y');
if($userDay >= 28 && $thisMonth == 2){
$userDay = 28;
}
while(!checkdate($thisMonth,$userDay,$thisYear)){
$thisMonth++;
if($thisMonth == 13){
$thisMonth = 1;
$thisYear++;
}
}
$return = strtotime($thisYear.'-'.$thisMonth.'-'.$userDay);
}
return $return;
}
// usage
echo date('Y-m-d',nextDate(29));
We get the user's choice and compare it today.
If today is less than or equal to user choice, we return the timestamp for this month.
If today is greater than user choice, we loop through dates, adding a month (or a year if it's $thisMonth hits 13). Once this date does exist again, we have our answer.
We check the dates using php's checkdate function, strtotime and date.
I really don't understand the question completely. You can easily determine the date for next 30 days for example
$next_ts = time() + 30 * 86400; // add 30 days to current timestamp
$next = date('Y-m-d', $next_ts); // format string as Y-m-d
echo $next;
If that is not what you need, please explain the problem.