Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert current year variable into PHP array

Tags:

php

wordpress

Here is the code I have in WordPress template's PHP file with some variables. Actually it is a stripped down replica of Warp Framework's warp/config.php.

<?php
return array(
    'month' => 'Current month',
    'year' => 'Current year'
);

Is it possible to replace Current month and Current year texts with actual PHP date output?

I know how to implement PHP date into a PHP page but I have no idea how to place it here.

like image 623
YKKY Avatar asked Sep 29 '26 18:09

YKKY


2 Answers

Try this

<?php
    return array(
        'month' => date('m'),
        'year' => date('Y')
    );
?>

That should give you what you need. Basically, using PHP's date() function, you can output the current date in whichever format you need by specifying the format mask in it's instantiation.

This page of the PHP.net site will help explain the many different methods of creating a date. PHP.net date() Function

like image 84
guyver4mk Avatar answered Oct 01 '26 06:10

guyver4mk


If the previous code creates an array called for instance $date and you just want to change it after it was created, you could do so as follows:

$date['month'] = date('m');
$date['year'] = date('Y');

If however you want to modify the code that is creating the array, then guyver4mk's answer is on point:

<?php
return array(
    'month' => date('m'),
    'year' => date('Y')
);
?>
like image 30
Webeng Avatar answered Oct 01 '26 08:10

Webeng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!