I have following data:
Array
(
[month] => 07
[year] => 2014
)
I want start & end timestamp of july 2014 using above array.
Used functions:
http://php.net//manual/ru/function.cal-days-in-month.php
http://php.net/manual/ru/function.mktime.php
<?php
$array = array('Month' => 07, 'Year' => 2014);
$daysCount = cal_days_in_month(CAL_GREGORIAN, $array['Month'], $array['Year']); //obtain month days count
$firstDay = mktime(0, 0, 0, $array['Month'], 1, $array['Year']); //obtain timestamp of specified month first day
$lastDay = mktime(0, 0, 0, $array['Month'], $daysCount, $array['Year']); //obtain timestamp of specified month last day
echo 'First: '.$firstDay.' Last: '.$lastDay;
?>
Perhaps something like this:
$start_month_str = $array['year'] . '-' . $array['month'] . '-1 midnight';
$end_month_str = $start_month_str . ' + 1 month - 1 minute';
$start_month_stamp = strtotime($start_month_str);
$end_month_stamp = strtotime($end_month_str);
See demo
http://php.net/manual/en/function.strtotime.php
Related
I have the following code in order to create a previous and next month links:
$date = mktime( 0, 0, 0, date("m"), 1, date("y") );
$nextMonth = strftime( '%B', strtotime( '+1 month', $date ) );
$prevMonth = strftime( '%B', strtotime( '-1 month', $date ) );
$nextYear = strftime( '%Y', strtotime( '+1 month', $date ) );
$prevYear = strftime( '%Y', strtotime( '-1 month', $date ) );
Now when i get to december
the next year variable 2016
the previous year variable 2015
But when the date is now january 2016
the next year variable 2015
the previous year variable 2014
I can't understand why its doing that or what i am doing wrong here. Any one have any idea how to fix this?
Testing your function with fixed parameters, you'll get :
$date = mktime( 0, 0, 0, 1, 1, 2016);
and echo $nextYear will not give you 2016 because you only added 1 month. Testing it does what's scripted :
the next year variable 2016 // which is january 2016 + 1 month = february 2016
the previous year variable 2015 // jan 2016 - 1 month = dec 2015
My guess is you are passing date('Y') without realizing it's 2015
I trying get something like this
16 Jul 2014, 12:00 am - 31 Jul 2014, 11:59 pm
How do I achieve it, I know I can use the strtotime feature with $date, but how do I get the half of a month such as 1-15 , 16-31 or maybe 16-30 (depend on some months)
$newDate = strtotime('+15 days',$date)
Thanks for helping!!
Could be done like so:
<?php
$a_date = "2014-7-24"; // calculate from given date (month does count for total days)
$last_day = date("t", strtotime($a_date)); // 31
$median = floor($last_day * .5); // 15
echo "1-" . $median; // 1-15
echo $median + 1 . "-" . $last_day; // 16-31
?>
Edit
Added function:
function split_date($date) {
$last_day = date("t", strtotime($date));
$median = floor($last_day * .5);
return array("last_day" => $last_day, "median" => $median);
}
Return an array: Array ( [last_day] => 31 [median] => 15 )
Is there a way in PHP to print the month name and year in french when we have a number in this format 52013 or 42013 ?
I have tried:
$month = 42013;
$monthNr = substr($month, -5, 1);
$timestamp = mktime(0, 0, 0, $monthNr, 1);
$monthName = date('M', $timestamp );
echo $monthName;
but the result is Apr
I'd like to get Avril 2013
you could do something like a simple array of your month names
$months = array(1 => 'Janvier', 2 => 'FĂ©vrier'...);
Then just look up the month value
$monthName = $months[date('n', $timestamp )];
Or based on comments suggestion (this is based on the answer in the linked question)
setlocale(LC_ALL, 'fr_FR');
echo strftime('%B', $timestamp);
How can I get last year's start and end date using PHP code? Is it possible?
The first day is always January 1, the last day is always December 31. You're really only changing the year attached to it. Depending on how you want the date formatted, you have a couple possibilities...
If you just want to display the physical date:
$year = date('Y') - 1; // Get current year and subtract 1
$start = "January 1st, {$year}";
$end = "December 31st, {$year}";
If you need the timestamp for both those dates:
$year = date('Y') - 1; // Get current year and subtract 1
$start = mktime(0, 0, 0, 1, 1, $year);
$end = mktime(0, 0, 0, 12, 31, $year);
Very simple stuff. You can manually specify which year if you wanted too. The premise is the same.
You can do it by using the below. Hope it helps someone.
//to get start date of previous year
echo date("d-m-y",strtotime("last year January 1st"));
//to get end date of previous year
echo date("d-m-y",strtotime("last year December 31st"));
start date of the year :
mktime(0,0,0,1,1,$year);
end date of the year :
mktime(0,0,0,1,0,$year+1);
Check this Stuff
$currentY = date('Y');
$lastyearS = mktime(0, 0, 0, 1, 1, $currentY-1 )."<br/>";
$lastyearE = mktime(0, 0, 0, 12, 31, $currentY-1 )."<br/>";
echo date('Y-m-d',$lastyearS)."<br/>";echo date('Y-m-d',$lastyearE);
Suppose if your current month is February or the month which has 30 days
echo date('Y-12-t', strtotime(date('Y-m-d'))); // if current month is february (2015-02-01) than it gives 2015-02-28
will give you inaccurate results
Solution:
So to get accurate result for the end date of an year, try the code below
$start_date = date("Y-01-01", strtotime("-1 year"));// get start date from here
$end_date = date("Y-12-t", strtotime($start_date));
(OR)
$last_year_last_month_date = date("Y-12-01", strtotime("-1 year"));
$end_date = date("Y-12-t", strtotime($last_year_last_month_date));
I have a variable with the following value
$month = 201002;
the first 4 numbers represent the year, and the last 2 numbers represent the month. I need to get the last 2 numbers in the month string name eg. Feb
My code looks like this
<?php echo date('M',substr($month,4,6)); ?>
I can I go about to obtain the month name
Append "01" and strtotime will be able to parse the string :
echo date('M', strtotime($month . '01'));
The second parameter of date is a timestamp.
Use mktime to create one.
$month = 201002;
$monthNr = substr($month, -2, 2);
$timestamp = mktime(0, 0, 0, $monthNr, 1);
$monthName = date('M', $timestamp );
This may help you..
$month = substr($month, -2, 2);
echo date('M', strtotime(date('Y-'. $month .'-d')));
You can use the DateTime Class to get a Date data structure using date string and format. Then get a date string with any format like this:
$month = 201002;
$date = DateTime::createFromFormat('Yd', $month);
$monthName = $date->format('M'); // will get Month name
$mydate = "201002";
date('M', mktime(0, 0, 0, substr($mydate, 4, 2), 1, 2000));
Being a programmer, and even knowing nothing of PHP data magic, I'd made it
$month = intval(substr($input_date,4,2));
$mons = explode(" ","Zer Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
echo $mons[$month];