Calculate last month, last 3 months, and last 6 months from today
I am looking for some ideas on how this could be improved:
switch ($periodValue) {
case "lastmonth":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
break;
case "last3month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
break;
case "last6month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
break;
How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?
php datetime
add a comment |
I am looking for some ideas on how this could be improved:
switch ($periodValue) {
case "lastmonth":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
break;
case "last3month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
break;
case "last6month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
break;
How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?
php datetime
add a comment |
I am looking for some ideas on how this could be improved:
switch ($periodValue) {
case "lastmonth":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
break;
case "last3month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
break;
case "last6month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
break;
How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?
php datetime
I am looking for some ideas on how this could be improved:
switch ($periodValue) {
case "lastmonth":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
break;
case "last3month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
break;
case "last6month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
break;
How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?
php datetime
php datetime
edited Sep 10 '14 at 14:12
Jamal♦
30.2k11116226
30.2k11116226
asked Sep 25 '12 at 18:50
John Magnolia
158116
158116
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try the DateTime class. In combination with the DateInterval class you can do something like this:
$until = new DateTime();
$interval = new DateInterval('P2M');//2 months
$from = $until->sub($interval);
echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');
Also, you have to set the $until
variable only once, before the switch statement.
add a comment |
The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()
?
$period = 30;//90 for 3 months, 180 for 6 months
$until = strtotime( 'now' );
$from = strtotime( "-$period days" );
//or
$period = 'last month';//'last 3 months', or 'last 6 months'
$from = strtotime( $period );
I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime()
is a pretty smart function, so who knows. Hope it helps.
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
add a comment |
$year = $data['year'];
$month = $data['month'];
$last_three_month_total_day = 0;
for ( $i=0; $i <= 2 ; $i++ ){
$last_three_month_total_day += cal_days_in_month(CAL_GREGORIAN,$month,$year);
$month = $month -1 ;
if($month == 0){ $month = 12; $year = $year-1 ;}
}
$result['last_three_month_total_day'] = $last_three_month_total_day;
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f15923%2fcalculate-last-month-last-3-months-and-last-6-months-from-today%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try the DateTime class. In combination with the DateInterval class you can do something like this:
$until = new DateTime();
$interval = new DateInterval('P2M');//2 months
$from = $until->sub($interval);
echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');
Also, you have to set the $until
variable only once, before the switch statement.
add a comment |
Try the DateTime class. In combination with the DateInterval class you can do something like this:
$until = new DateTime();
$interval = new DateInterval('P2M');//2 months
$from = $until->sub($interval);
echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');
Also, you have to set the $until
variable only once, before the switch statement.
add a comment |
Try the DateTime class. In combination with the DateInterval class you can do something like this:
$until = new DateTime();
$interval = new DateInterval('P2M');//2 months
$from = $until->sub($interval);
echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');
Also, you have to set the $until
variable only once, before the switch statement.
Try the DateTime class. In combination with the DateInterval class you can do something like this:
$until = new DateTime();
$interval = new DateInterval('P2M');//2 months
$from = $until->sub($interval);
echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');
Also, you have to set the $until
variable only once, before the switch statement.
edited Sep 10 '14 at 9:17
Anil Jaiswal
33
33
answered Sep 25 '12 at 20:59
Dirk McQuickly
20114
20114
add a comment |
add a comment |
The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()
?
$period = 30;//90 for 3 months, 180 for 6 months
$until = strtotime( 'now' );
$from = strtotime( "-$period days" );
//or
$period = 'last month';//'last 3 months', or 'last 6 months'
$from = strtotime( $period );
I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime()
is a pretty smart function, so who knows. Hope it helps.
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
add a comment |
The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()
?
$period = 30;//90 for 3 months, 180 for 6 months
$until = strtotime( 'now' );
$from = strtotime( "-$period days" );
//or
$period = 'last month';//'last 3 months', or 'last 6 months'
$from = strtotime( $period );
I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime()
is a pretty smart function, so who knows. Hope it helps.
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
add a comment |
The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()
?
$period = 30;//90 for 3 months, 180 for 6 months
$until = strtotime( 'now' );
$from = strtotime( "-$period days" );
//or
$period = 'last month';//'last 3 months', or 'last 6 months'
$from = strtotime( $period );
I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime()
is a pretty smart function, so who knows. Hope it helps.
The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()
?
$period = 30;//90 for 3 months, 180 for 6 months
$until = strtotime( 'now' );
$from = strtotime( "-$period days" );
//or
$period = 'last month';//'last 3 months', or 'last 6 months'
$from = strtotime( $period );
I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime()
is a pretty smart function, so who knows. Hope it helps.
answered Sep 25 '12 at 20:19
mseancole
5,8731926
5,8731926
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
add a comment |
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
– deathemperor
May 31 at 9:55
add a comment |
$year = $data['year'];
$month = $data['month'];
$last_three_month_total_day = 0;
for ( $i=0; $i <= 2 ; $i++ ){
$last_three_month_total_day += cal_days_in_month(CAL_GREGORIAN,$month,$year);
$month = $month -1 ;
if($month == 0){ $month = 12; $year = $year-1 ;}
}
$result['last_three_month_total_day'] = $last_three_month_total_day;
New contributor
add a comment |
$year = $data['year'];
$month = $data['month'];
$last_three_month_total_day = 0;
for ( $i=0; $i <= 2 ; $i++ ){
$last_three_month_total_day += cal_days_in_month(CAL_GREGORIAN,$month,$year);
$month = $month -1 ;
if($month == 0){ $month = 12; $year = $year-1 ;}
}
$result['last_three_month_total_day'] = $last_three_month_total_day;
New contributor
add a comment |
$year = $data['year'];
$month = $data['month'];
$last_three_month_total_day = 0;
for ( $i=0; $i <= 2 ; $i++ ){
$last_three_month_total_day += cal_days_in_month(CAL_GREGORIAN,$month,$year);
$month = $month -1 ;
if($month == 0){ $month = 12; $year = $year-1 ;}
}
$result['last_three_month_total_day'] = $last_three_month_total_day;
New contributor
$year = $data['year'];
$month = $data['month'];
$last_three_month_total_day = 0;
for ( $i=0; $i <= 2 ; $i++ ){
$last_three_month_total_day += cal_days_in_month(CAL_GREGORIAN,$month,$year);
$month = $month -1 ;
if($month == 0){ $month = 12; $year = $year-1 ;}
}
$result['last_three_month_total_day'] = $last_three_month_total_day;
New contributor
New contributor
answered 8 mins ago
pradip kor
101
101
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f15923%2fcalculate-last-month-last-3-months-and-last-6-months-from-today%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown