Solved

PHP: retrieve month and year of the next month

Posted on 2014-01-08
15
766 Views
Last Modified: 2014-01-09
Hi,

I need a PHP function that will give me the written month (in Dutch) + Year of the next month (based on current date).

Something like GetMonthYear(Date);  // today is January 8th 2014

Result:
Februari 2014   (dutch for February 2014)

Translation of the month can probably be done in an array.

Found this function on the web to increase the month.
<?php 
function add_date($orgDate,$mth){ 
  $cd = strtotime($orgDate); 
  $retDAY = date('Y-m-d', mktime(0,0,0,date('m',$cd)+$mth,date('d',$cd),date('Y',$cd))); 
  return $retDAY; 
} 
?>

Open in new window

0
Comment
Question by:Delphiwizard
  • 7
  • 6
  • 2
15 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
This should do it
<?php
$month = array (
	'Januari',
	'Februari',
	'Maart',
	'April',
	'Mei',
	'Juni',
	'Juli',
	'Augustus',
	'September',
	'Oktober',
	'November',
	'December'
);

$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo $month[intval($date->format('m')) - 1] . ' ' . $date->format('Y');
?>

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Here is a better solution using setlocale instead of the array
<?php
setlocale(LC_TIME, 'nld_NLD');

$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo strftime("%B %Y", strtotime($date->format('Y-m')));
?>

Open in new window

0
 

Author Comment

by:Delphiwizard
Comment Utility
Thanks.
Can you show me how I can use this as a function?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
If you want the month in Dutch (NL) or a couple of other languages, this will do the job.  PHP setLocale() means well but can be tricky to get right.  The man page says, " Different systems have different naming schemes for locales."  There are some illuminating and cautionary user-contributed notes on the man page!

Lines 14-93 translate the months reliably
Lines 97-110 defines the GetMonthYear() function

Your script will need to include both functions.

<?php // RAY_temp_delphiwizard.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28333185.html
// REF http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
// REF http://www.omniglot.com/language/time/months.htm

// THIS RETURNS IN ENGLISH - INVALID LOCALE?  NO MESSAGE AT ALL!
setlocale(LC_TIME, 'nld_NLD');
$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo strftime("%B %Y", strtotime($date->format('Y-m')));

// MONTHS IN DIFFERENT LANGUAGES
function myMonth($lang='EN', $date='TODAY')
{
    // CONSTRAIN MONTH NUMBER
    $ts = strtotime($date);
    if (!$ts) $ts = time();
    $m = date('m', $ts);

    $lang = strtoupper(substr(trim($lang),0,2));

    // MONTHS IN LANGUAGES
    $months = array
    ( 'NL' => array
      ( '01' => 'januari'
      , '02' => 'februari'
      , '03' => 'maart'
      , '04' => 'april'
      , '05' => 'mei'
      , '06' => 'juni'
      , '07' => 'juli'
      , '08' => 'augustus'
      , '09' => 'september'
      , '10' => 'oktober'
      , '11' => 'november'
      , '12' => 'december'
      )
    , 'FR' => array
      ( '01' => 'janvier'
      , '02' => 'février'
      , '03' => 'mars'
      , '04' => 'avril'
      , '05' => 'mai'
      , '06' => 'juin'
      , '07' => 'juillet'
      , '08' => 'août'
      , '09' => 'septembre'
      , '10' => 'oktobre'
      , '11' => 'novembre'
      , '12' => 'decembre'
      )
    , 'DE' => array
      ( '01' => 'Januar'
      , '02' => 'Februar'
      , '03' => 'März'
      , '04' => 'April'
      , '05' => 'Mai'
      , '06' => 'Juni'
      , '07' => 'Juli'
      , '08' => 'August'
      , '09' => 'September'
      , '10' => 'Oktober'
      , '11' => 'November'
      , '12' => 'Dezember'
      )
    , 'IT' => array
      ( '01' => 'gennaio'
      , '02' => 'febbraio'
      , '03' => 'marzo'
      , '04' => 'aprile'
      , '05' => 'maggio'
      , '06' => 'giugno'
      , '07' => 'luglio'
      , '08' => 'agosto'
      , '09' => 'settembre'
      , '10' => 'ottobre'
      , '11' => 'novembre'
      , '12' => 'dicembre'
      )
    )
    ;

    // IF ENGLISH
    if ($lang == 'EN') return date('F', $ts);

    // IF UNKNOWN LANGUAGE
    if (!array_key_exists($lang, $months)) return date('F', $ts);

    // IF KNOWN LANGUAGE
    return $months[$lang][$m];
}


// FOR THE ANSWER AT EE
function getMonthYear($date='TODAY')
{
    // CONSTRAIN MONTH NUMBER
    $ts = strtotime($date);
    if (!$ts) $ts = time();

    // NEXT MONTH (MAYBE NEXT YEAR)
    $newdt = date("Y-m-01", $ts);
    $newts = strtotime($newdt . ' + 1 MONTH');
    $newmo = date('m', $newts);
    $newyr = date('Y', $newts);

    return myMonth('NL', $newyr . '-' . $newmo . '-01') . " $newyr";
}

// TEST THE FUNCTION
echo '<pre>';
echo PHP_EOL . getMonthYear();
echo PHP_EOL . getMonthYear('December, 2014');
echo PHP_EOL . getMonthYear('TODAY - 78 WEEKS');

Open in new window

HTH, ~Ray
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
As your initial requirement says to use the current date - here is a function that does what you require.
function get_month_year()
{
  setlocale(LC_TIME, 'nld_NLD');

  $date = new DateTime();
  $date->add(new DateInterval('P1M'));
  return strftime("%B %Y", strtotime($date->format('Y-m')));
?>
}

Open in new window

Re the comments on setLocale - it does require some tweaking depending on whether you are running on windows or Linux - but you should know which system you are runnign on and it is a trivial thing to be able to set the correct value. You could even write a bit of code that auto detects and calls the right parameter.
Either way - a lot better than having to maintain an array of months.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
@Ray
// THIS RETURNS IN ENGLISH - INVALID LOCALE?  NO MESSAGE AT ALL!
setlocale(LC_TIME, 'nld_NLD');

That works on Windows systems

For Linux

setLocale(LC_TIME, 'nl_NL')

Should do the trick
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yes, that's why I wrote...
The man page says, " Different systems have different naming schemes for locales."  There are some illuminating and cautionary user-contributed notes on the man page!
Also, this is kind of a grin... When was the last time you needed an update to the list of months?  Not in my lifetime, IIRC :-)
better than having to maintain an array of months
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Closing Comment

by:Delphiwizard
Comment Utility
@Ray:This solution has much more code, though I can strip the other languages. Most important it works without me having to make any changes.
@JulianH: Your code gives me just an errormessage and I believe that local settings might influence the outcome, where Ray's code will always produce the same result.
Thank you both.
0
 

Author Comment

by:Delphiwizard
Comment Utility
@JulianH: the error I get is:
Fatal error: Call to undefined method DateTime::add() in /home/mydomain/domains/mysite.nl/public_html/includes/functions/functions.shopcart.php on line 269
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
If you are using a version of PHP < 5.2 then that would explain the issue.
0
 

Author Comment

by:Delphiwizard
Comment Utility
@JulianH: I don't know which version is installed. It's maintained by the webhosting company. I'm in the impression that they keep the servers updated with the latest versions, but that might not always be the case. Thank you for the info.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
To find out the version create this file
pi.php
<?php
phpinfo();
?>

Open in new window

Upload and call that script - version will be displayed at the top of the page. If running on less than 5.2 you might want to check if you can upgrade - PHP currently at version 5.4

Here is a link to the code on a windows server. The script has an extra line to determine OS and set locale string correctly

And here is the code running at that site.
<?php
// Set locale string based on OS
$locale = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'nld_NLD' : 'nl_NL';
setlocale(LC_TIME, $locale);

$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo strftime("%B %Y", strtotime($date->format('Y-m')));
?>

Open in new window

Slightly less longwinded than the accepted solution but does require 5.2 or higher
0
 

Author Comment

by:Delphiwizard
Comment Utility
PHP Version 5.2.17
Running the new script still gives the error.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
0
 

Author Comment

by:Delphiwizard
Comment Utility
Thank you. Now we know what went wrong...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to us ob_start in a function 11 33
How do I edit this Drupal page? 9 26
Button Click 11 23
PHP Sum Column in Table 3 26
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now