I have save year as input into database but it save the duration like if I get input 1990 as input in database save 26.Here 26 is difference of current date to selected date 2016-1990=26.I am using php Date function but it's not work proper.Using this get fixed year 1970
<?php
$dr = [];
for($i = date("Y"); $i > date("Y") - 100; $i--) {
$dr[] = $i;
}
echo $form->field($model, 'ven_established_date')->dropDownList($dr);//Here Select year
?>
$date = $model->ven_established_date;
echo '$date'.$date;
$year = date("Y", strtotime($date));
echo '$year'.$year;
As your question is not much clear so what i understood from your question is you have a year count(ex:-26) and you want exact year.
Solution:-
$yearCount = 26; //input you have, getted from DB
$currentYear = date("Y");
$pastYear = $currentYear - $yearCount;
echo $pastYear;
Related
I can display dates from start to end date from stored data in mysql, but I want to display current month dates from 1st to end date of this month in form of
1
2
3
4
.
.
.
.
.
31
Is this possible?
Refer to PHP cal_days_in_month
As explained here
This function will return the number of days in the month of year for the specified calendar.
int cal_days_in_month ( int $calendar , int $month , int $year )
And an example:
$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There were {$number} days in August 2003";
Use a loop to display a count of the number of days
For the PHP part, this might help you:
// Get the current date
$today = getdate();
// Get the number of days in current month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $today['mon'], $today['year']);
// Print the dates
for ($i = 1; $i <= $days_in_month; $i++) {
echo ' ' . $i;
}
Styling and output is another task, this is just to get you started.
yes. it is possible.
please, use below php code. it can work for php 4.1 and higher.
<?php
$number = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
for($i=1;$i<=$number;$i++)
echo $i.'<br>';
?>
If you want all days in the month, try this loop where date("t") give you the numerical last day of the month, and we know the first day is always 1.
$last = date("t");
for($i=1; $i<= $last; $i++) echo "$i ";
I have the following code so far:
$months = array();
$numJoin = date("n",strtotime($me['joinTime']));
$numLast = date('n', strtotime('Dec 31'));
$numCurrent = date("n",strtotime('2016-06-01'));
array_push($months, date("F", strtotime($me['joinTime'])));
for($i = ($numJoin + 1); $i <= $numLast; $i++) {
if($numCurrent>$numJoin) {
$dateObj = date_create_from_format('!m', $i);
array_push($months, $dateObj->format('F'));
}
$numCurrent= -1;
}
What I'm trying to do here is to add into the array current month that kicks in, and save previous months in the array like for example:
Start month is -> May
June kicks in -> I add June into the array (now I should have May and June in array).
July kicks in -> I add July into the array (now I should have May, June and July in array).
How can I do this achieve this? Current solution works only for +1 month.. I can't add more than 1 month :/
P.S. New item should only be added when the new month kicks in, and previous content of the array should be saved...
Here we go, you need to check that your month is less than the current month or not. Check Online
$months = array();
$num = date("n",strtotime($me['joinTime'])); //join month number
$now = date("n"); //Current month number
for($i = $num; $i <= $now; $i++){
$dateObj = DateTime::createFromFormat('!m', $i);
array_push($months, $dateObj->format('F'));
}
print_r($months);
I'm still a little confused, but I think this is what you are after... all month names after the join month and until current month...
$me = array('joinTime'=>'2016-03-01');
$dtCurrent = strtotime($me['joinTime']);
$arrMonths = array();
while($dtCurrent < time()) {
$dtCurrent = strtotime("+1 month",$dtCurrent);
$arrMonths[] = date('F',$dtCurrent);
}
var_dump($arrMonths);
This question already has answers here:
PHP - How to get year, month, day from time string
(5 answers)
Closed 7 years ago.
I want to get the day number of a date.
For example my date is: 2015-12-31, it should return 365 and 2015-01-1 would be 1.
How can I do that?
EDIT: date('z') does not work for me, I dont want the current date
Use strtotime() — to parse about any English textual datetime description into a Unix timestamp. And getdate() — Get date/time information.
<?php
$day = getdate(strtotime("2015-12-31"));
echo $day['yday']+1; // output 365
?>
Try
<?php
$today = getdate(strtotime('2015-12-31'));
echo '<pre>';
print_r($today['yday'] + 1);
?>
Read GetDate
You can try it.
<?php
$day = 31;
$mon = 12;
$year = 2015;
$sum = 0;
$days = 0;
$month_day = array(31,28,31,30,31,30,31,31,30,31,30,31);
for ( $i = 0; $i < $mon; $i++){
$sum += $month_day[$i];
}
$days = $sum - ($month_day[$mon-1] - $day);
echo "$days";
?>
You may get day, mon, year from your expected date and make it integer. You should check the year is leap year or not. It may vary one day if it is a leap year. I think you shall do it.
How to Get previous days in this month with PHP?
ex : current date = 06/05/2015
previous days Show ==>
05/05/2015
04/05/2015
03/05/2015
02/05/2015
01/05/2015
cordially
Something like this:
$date = DateTime::createFromFormat("d/m/Y", "06/05/2015");
$previousDates = array();
$maxDay = $date->format("d");
for($i = 1; $i < $maxDay; $i++) {
$previousDates[] = $date->modify("-1 day")->format("d/m/Y");
}
Sowing previous 30 days from current days:
for($i=-1; $i>=-30;$i--)
echo date('d/m/Y',strtotime($i." days"));
You can change date format if you want.
<?php
$currentDate = date('d');
for($inc = 1;$inc <= ($currentDate-1);$inc++){
echo date('d/m/Y',strtotime("-".$inc." days")).'<br/>';
}
?>
Try above code. Hope this will help you.
Output:
05/05/2015
04/05/2015
03/05/2015
02/05/2015
01/05/2015
This is how you get a previous day using php:
$prev_day = date('d.m.Y',strtotime("-1 days"));
to get all the previous days from the specified day, you need to do a loop:
$currentday = date("d");
for($i=1; $i < $currentday ;$i++)
{
echo date('d.m.Y',strtotime($i."- days"))."<br/>";
}
How I can convert INT to date in php and than find this date in mysql table ?
Lets say I want to convert
$month = (int)$_GET['month'];
$month = 42014 ;//This means 04th month in 2014 year
$month = 04-2014; // I want this
And than find that in mysql table
$query=mysql_query("SELECT id FROM matches WHERE date=$month");// This need to select
// all data that is
// created in 04th month
// of 2014 year .
echo "Thanks :)";
Don't pass $month parameter like you do.
Send and pass both $month and $year and handle them after:
$month = (int)$_GET['month'];
$year = (int)$_GET['year'];
if($month < 10) { // add this check for months lesser then october (they containt 1 digit, which is wrong for t-sql)
$month = "0".$month;
}
$query=mysql_query("SELECT id FROM matches WHERE DATE_FORMAT(datefield, '%m-%Y') = '$month-$year'");
source
If you can't send both month and year in different variables, do this, like M Khalid Junaid suggested:
$query=mysql_query("SELECT id FROM matches WHERE DATE_FORMAT(datefield,'%m%Y')='$month'");
Try as below using DateTime::createFromFormat
$month = $_GET['month'];
$date = DateTime::createFromFormat('mY',$month);
echo $date->format('m-Y');
Try this
$input=42014;
$year = strlen(substr($input,-4));
$month = substr(0,(strlen($input)-strlen($year)));
$final = $month."-".$year