Convert month number to month short name - php

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];

Related

How to get the month name in French and year from a number

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 convert date from 8 to AUG

I would like to convert the number to actual month. I only need a month.
I am now trying like this
$str = 8;
$anInt = intval($str);
$test = date('M',$anInt);
echo $test;
the result is JAN, I was supposed to get "AUG" as a result. I dont know why.
any idea or suggestion for me?
The date() function assumes a timestamp.
You should try this:
$test = date('M',mktime(0,0,0,$anInt,0,0);
From the docs for date and mktime:
// Prints: Your date's month is : August
echo "Your date's month is : " . date("F", mktime(0, 0, 0, $anInt, 0, 0));
Use mktime.
echo date('M', mktime(0, 0, 0, $str));
Here you are:
echo date('M',strtotime('08/01/2012'));
Or, if you want all caps:
echo strtoupper(date('M',strtotime('08/01/2012')));
There might be other approaches to this, but this was the first to come to mind
Duplicate question. Read here.
Read about date function here.
using date() you should use timestamp.
To convert date value to timestamp, use strtotime
$date = '2012-8-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);
For your problem:
$monthnumber = 4;
$date = '2012-'.$monthnumber.'-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);

Getting the last occurrence of a day/month in PHP

I'm wondering if there's an easy way (like using strtotime) whereby I can get the unix time for the last occurrence of a day/month combination. For example, if I was to ask for "1st of September" today (9th May 2012) I would get 1314835200 (1st Sep 2011), but if the code was to run again this October, I would get 1346457600 (1st Sep 2012), and the same if I ran it 1 year from now.
Being able to do it forwards as well as backwards would be a massive bonus.
$month = 9;
$day = 1;
$timestamp = mktime(0, 0, 0, $month, $day);
if ($timestamp > time()) {
$timestamp = mktime(0, 0, 0, $month, $day, date('Y') - 1);
}

Php how to go from day of the year to date and vice versa

How do you go in php from a nth day in the year to the date like:
getdatefromday(275, 2012)
and it outputs a date (better if an object).
And I'd like to do the opposite too, like getdayoftheyear("21 oct 2012")
This is pretty easy all around. You should read up on the DateTime object's createFromFormat static method here, the date function here and the strtotime function here.
// This should get you a DateTime object from the date and year.
function getDateFromDay($year, $dayOfYear) {
$date = DateTime::createFromFormat('z Y', strval($year) . ' ' . strval($dayOfYear));
return $date;
}
// This should get you the day of the year and the year in a string.
date('z Y', strtotime('21 oct 2012'));
Try(days starts from 0 not 1):
$date = DateTime::createFromFormat( 'Y z' , '2012 275');
var_dump($date);
and that:
echo date('z', strtotime('21 oct 2012'));
$todayid = date("z"); // to get today's day of year
function dayofyear2date( $tDay, $tFormat = 'd-m-Y' ) {
$day = intval( $tDay );
$day = ( $day == 0 ) ? $day : $day - 1;
$offset = intval( intval( $tDay ) * 86400 );
$str = date( $tFormat, strtotime( 'Jan 1, ' . date( 'Y' ) ) + $offset );
return( $str );
}
echo dayofyear2date($todayid);
day of year
I know it's a bit old and the answer has already been accepted, but I wanted to put here an alternative way to do it, trying to use the exact formats required by the question owner:
// This gets the day of the year number
// given a formatted date string like
// "21 oct 2012"
function getdayoftheyear($dateString) {
date('z', strtotime($dateString));
}
The other way around:
// This gets the date formatted in $dateFormat way
// like "Y-m-d"
// given the $dayOfTheYear and the $year in numeric form
function getdatefromday($dateFormat, $dayOfTheYear, $year) {
date($dateFormat, mktime(0, 0, 0, 1, ($dayOfTheYear + 1), $year));
}
This second function uses a feature of mktime(), allowing to set whatever numbers in parameters list, because it manages overflows finding by itself the right month.
So if you call mktime(0, 0, 0, 1, 32, 2015) it actually knows that the 32nd day of the 1st month is the 1st day of the 2dn month, and so on.
You can use strtotime() to get the amount of seconds for the year value ( http://de2.php.net/manual/en/function.strtotime.php). Than at the amount of days in seconds (day * 24 * 60 * 60). Now you can use this value with date() (see first answer)
function DayToTimestamp($day, $year = null)
{
isset($year) or $year = date('Y');
return strtotime("1 Jan $year +$day day");
}
function getDateFromDayOfYear($dayOfYear,$year){
return date('Y-m-d', strtotime('January 1st '.$year.' +'.$dayOfYear.' days'));
}
Getting the day of the year is easy. Just use the date function with the correct parameter as documented in the manual (it returns 0 for Jan-1 to 365 for Dec-31 on a leap year).
Going the other way will need a bit of creativity.

How do I get last year's start and end date?

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));

Categories