i am a newbie of php, supposed my blog created on 2011 year 1 month.and the article stored time as this 1305357473. now i want to use a function, which can output the before year and moth as this.
2011 year 1 month
2011 year 2 month
2011 year 3 month
2011 year 4 month
2011 year 5 month
......
i want to make the function can output the current and the past year and month.
this is my function and output the result. but i don't know how to finish.
function outYearMonth($year,$month){
$year=date(Y);
$past_month=strtotime(date(1)) ;
$month=strtotime(date(m));
$jg_month=round($month - $past_month);
$month=array();
for($i==0;$i<=$jg_month;$i++){
$month[]= date(1)+1;
}
}
This code:
$months = 10; //count of months
$date = date_create( 'now' );
echo date_format( $date, 'Y M' );
for ( $i = 0; $i < $months; $i++ ) {
date_sub( $date , date_interval_create_from_date_string( '1 months' ) );
echo date_format( $date, 'Y M' );
}
Will output:
2011 May
2011 Apr
2011 Mar
2011 Feb
2011 Jan
2010 Dec
2010 Nov
2010 Oct
2010 Sep
2010 Aug
2010 Jul
echo date('Y m');
will give you this year and month
echo date('Y m',strtotime('last month',time()))
will give you last month
for looping through all the month just get the current month and make a loop
$currentMonth = (int) date('m',time());
$currentYear = date('Y',time());
for($i=0; $i <= $currentMonth; $i++) {
echo "$currentYear $i";
}
will echo all months
2011 1
2011 2
2011 3
2011 4
2011 5
Related
I am trying to get the last six months from the current date in PHP. It's a simple problem, but my code is not giving the right result.
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month"));
echo "<br>";
}
And the output is
March 2018
March 2018
January 2018
December 2017
December 2017
October 2017
I dont understand why March is coming twice.
Because strototime('-1 month') doesn't handle correctly the end of month.
You could use the first day of the current month:
$dt = strtotime(date('Y-m-01'));
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month", $dt));
echo "<br>";
}
Outputs:
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
i want to print all date from today to specific day this specific day will be taken from database
$sql=mysqli_query($conn,"select * from tbl_activities where db_id='$id'")or die(mysqli_error($conn));
$row=mysqli_fetch_array($sql);
$day=$row['db_day'];
this code give me number of date example 10 days
i want to print date from today to 13 days
for($i=1;$i<=$day;$i++){
}
output will be like this
Thursday 21st of July 2016
Friday 22st of July 2016
23st of July 2016
24st of July 2016
25st of July 2016
26st of July 2016
27st of July 2016
28st of July 2016
29st of July 2016
30st of July 2016
31st of July 2016
1st of August 2016
try my edited code.
$day=$row['db_day'];
$start_day = strtotime(date("Y-m-d"));
$end_day = $start_day + 86400 * $day;
while($end_day >= $start_day){
echo date("j F, Y",$start_day)."\r\n";
$start_day = $start_day + 86400;
}
I hope its helps
<?php
$date = date('Y-m-d');
$end_date = date('Y-m-d', strtotime('+9 days')); //add the num of days u need here
while (strtotime($date) <= strtotime($end_date)) {
echo "<pre>".$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
You could use the Datetime class and it's associated methods
/* formats for dates */
$df='Y-m-d H:i:s';
$dfo='l jS \of F, Y';
/* variable, number of days from NOW */
$days=14;
/* set the timezone and date interval */
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('P1D');
/*establish date range */
$ts=date( $df, strtotime('yesterday') );
$tf=date( $df, strtotime('1st August 2016') );
/* Alternatively... */
$tf=date( $df, strtotime('now +'.$days.' days') );
/* create instances of the `Datetime` class for both dates */
$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );
/* show the dates in the range */
while( $start->add( $interval ) <= $end ){
echo $start->format( $dfo ). '<br />';
}
Will output:
Thursday 21st of July, 2016
Friday 22nd of July, 2016
Saturday 23rd of July, 2016
Sunday 24th of July, 2016
Monday 25th of July, 2016
Tuesday 26th of July, 2016
Wednesday 27th of July, 2016
Thursday 28th of July, 2016
Friday 29th of July, 2016
Saturday 30th of July, 2016
Sunday 31st of July, 2016
Monday 1st of August, 2016
Tuesday 2nd of August, 2016
Wednesday 3rd of August, 2016
Thursday 4th of August, 2016
I am displaying month titles 3 month into the future as well as getting the 1st and last day of each of those months.
for($i = 1; $i < 4; $i++) { // For each month for 3 months
$monthTitle = date('F Y', strtotime('+'.$i.' month'));
$begin_date = date('Y-m-01', strtotime('+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime('+'.$i.' month')); // Last day of calendar months in future.
};
Nov. 29, 2015 output is:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
February 2016
2016-02-01
2016-02-29
This was working great right up until yesterday, Nov. 29, 2015 but today Nov. 30, 2015 it skips February.
Nov. 30, 2015 output is:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
March 2016
2016-03-01
2016-03-31
I'm guessing a bug but does anybody know of a work around?
Thanks to #devlin carnate for pointing me in the right direction.
for($i = 1; $i < 4; $i++) { # for each month
$tmp = date('Y-m-15'); // Get the middle of the month to avoid PHP date bug.
$begin_date = date('Y-m-01', strtotime($tmp . '+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime($begin_date)); // Last day of calendar months in future.
$monthTitle = date('F Y', strtotime($begin_date));
};
This seems to work very well.
You can use DateInterval to add one month to the current date, so you can get the first and the last day of month.
<?php
$date = new DateTime('2015-12-01');
$i = 0;
while($i < 3){
printf("%s | first day: %s, | last day: %s <br>", $date->format('F Y'), $date->format('d'), $date->format('t'));
$date->add(new DateInterval('P1M'));
$i++;
}
Output:
December 2015 - first day: 01, | last day: 31
January 2016 - first day: 01, | last day: 31
February 2016 - first day: 01, | last day: 29
if last day of next month is needed then you can use this
$d = new DateTime( '2010-01-31' );
$d->modify( 'last day of next month' );
echo $d->format( 'Y-m-d' ), "\n";
I have two queries, both related to dates.
1) I have dates in these formats, which I'm looking to normalise into the same format before saving into a database:
Saturday 26 July
Monday 28 - Wednesday 30 July
July 24th, 2014
Thu 4 Sep
Thu 28 Aug — Fri 19 Sep
24-07-2014
Single days are quite easy to work out using strtotime(), but ranges of dates are a bit more tricky.
This, for example, doesn't work:
$dateString = "Monday 28 - Wednesday 30 July";
if (strpos($dateString, "-")) {
$datePieces = explode("-", $dateString);
$startDate = strtotime($datePieces[0]);
$endDate = strtotime($datePieces[1]);
} else {
$startDate = strtotime($dateString);
$endDate = strtotime($dateString);
}
echo '<pre>';
echo date('d F Y', $startDate);
echo '<br/>';
echo date('d F Y', $endDate);
Because the month is only on one side of the explode(), doing it this way returns:
01 January 1970
30 July 2014
2) I need a way of working out what year the date is (it will always be in the future). Something along the lines of:
if (the month in the date string has elapsed) {
the year of the date is this year + 1
}
As long as each source provides you with a consistent format you can use DateTime() and DateTime::createFromFormat() to process the dates for you.
//Saturday 26 July
$date = DateTime::createFromFormat('l j F', 'Saturday 26 July');
//July 24th, 2014
$date = new DateTime('July 24th, 2014');
//Thu 4 Sep
$date = DateTime::createFromFormat('D j M', 'Thu 4 Sep');
//Thu 28 Aug — Fri 19 Sep
list($start, $end) = explode(' - ', 'Thu 28 Aug — Fri 19 Sep');
$start = DateTime::createFromFormat('D j M', $start);
$end = DateTime::createFromFormat('D j M', $end);
//24-07-2014
$date = new DateTime('24-07-2014');
I'm going to leave handling Monday 28 - Wednesday 30 July to you since you'll need to do a little more work to get the month from the second date and apply it to the first. But this should show you how to go about this.
I need to write a simple loop routine to show 24 months back starting with today's month. How would I do that?
$start = date(M) - 24;
$end = date(M);
foreach() {
echo ''; // Dec, Jan...
}
Something like this should work:
for($i = 1; $i <= 24; $i++) {
echo date("M", strtotime("-$i months")) . "\n";
}
Result
Feb
Jan
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
See a demo
for ($i = 1; $i <= 24; $i++) {
$months[] = date("Y-m%", strtotime( date( 'Y-m-01' )." -$i months"));
}
or
for full textual representation of month you need to pass "F":
echo date("y:F:d");
for previous month you can use
echo date("y:F:d",strtotime("-24 Months"))