How to write current and future year in link? - php

I have some links that I want to include the current year in, as well as the following year.
For example:
2016 to 2017
Instead of manually having to maintain the link, I would like to make the current year and future year dynamically print.
Can anyone tell me the best way to accomplish this? The page is PHP, so I wasn't sure if there's a easy way with PHP or jQuery, or something else.
I already tried <?php echo date("Y"); ?> and it didn't work on the front end (it was blank in the spot I was trying to get 2016 to display).

For both year
<?php
$current= new \DateTime();
$future = new \DateTime('+ 1 years');
?>
or you can use it with one line
$current = (new DateTime)->format("Y");
.
<?php echo $current->format('Y')." to ".$future ->format('Y');?>

You probably need something like:
<?php
$year = date("Y");
$nextYear = $year + 1;
echo <<< LOL
$year to $nextYear
LOL;
?>

Related

How to echo YEAR only from a php info containing including also day and month

I'm using PHP to send a complete date to another page (example 2021-02-23).
I'm calling this information 'year' but in the destination page I want it to display the year only (2021 in this case).
I am trying the following code, but it is actually printing me year a wrong '1970' for each value. How should I edit this in order to work correctly?
<?php $_GET['year']; echo date('Y', strtotime($date)); ?>
You can use this code to access the amount of the year
<?php
$date = $_GET['year']; // 2021-02-23 OR any DateTime
$year = explode('-', $date);
echo $year[0];
?>
I hope helped you.

Carbon dates find out if today is thursday or go back

I have the below code running in one of my scripts and it works well, but feels rather clunky and long. I feel like there might be a much shorter way of achieving the same result. I also mean without using a shorthand if statement.
I need to find out if today is thursday and if not use the previous thursday as the date. Any thoughts / ideas would be great.
<?php
if (new Carbon('this thursday') > new Carbon()) {
$date = Carbon('this thursday');
} else {
$date = Carbon('last thursday');
}
?>
According to
http://carbon.nesbot.com/docs/#api-modifiers :
The $date will hold the date to be shown.
<?php
$today = new Carbon();
if($today->dayOfWeek == Carbon::THURSDAY)
$date = $today;
else
$date = new Carbon('last thursday');
?>

PHP getdate() outputs only year, and not current

I have this php code that I want to output the current year, month and day.
date_default_timezone_set('Sweden/Stockholm');
$time_info = getdate();
$day = $time_info['mday'];
$month = $time_info["mon"];
$year = $time_info['year'];
$date = "$year, $month, $day";
And the output:
2014 10 29
But I want it to output
year-month-day
But when I change the $date to $date = $year."-".$month."-".$day."-"; the output is: 1975.
Obviously, this is wrong, so how can I fix it. And a explanation why this occurs would also be great.
EDIT:
Ok, so according to #Marc B and #Barry , it did at some point math, and they were right. I don't know why this occurs, but I got it sorted out. Thanks!
Why don't you use the php date function?
echo date("Y-m-d");
You can get that if you run this.
$time_info = getdate();
echo $time_info->format('Y-m-d');
Using PHP format() function resolves your problem.
Documentation is here.
use php date function Use Like this
date_default_timezone_set('Sweden/Stockholm');
echo date("Y-m-d");

PHP - Turn a timestamp (2012-07-12 20:26:00) Into two seperate variables that refelct the month and day values?

I would like to separately strip the day and month values out of this timestamp "2012-07-12 17:50:00".
So essentially I could have two variables like:
$month = 7
$day = 12
As I am not very proficient with php, specifically regex coding, I thought I would post this question to ask for advice as to how I would go about accomplishing something like this.
Any assistance, insight or input in this regard would be greatly appreciated. Thank you!
AS A NOTE: Futhermore, I would like to turn the two variables into an output like "12th July". This can be coded quite easily so I have not made this a part of the question but if there is a simple function that deals with this information would be much appreciated too!
As said in the comments, DateTime would be a good choice.
First, if your php config dosen't already set it up for you, set your default timezone by:
date_default_timezone_set('XXXX');
XXXX stand for a value out of the List of supported timezones
initialize your date by:
$DateTime = new DateTime();
depending on where you get the timestamp, lets assume you will create it out of PHP:
$timestamp = $DateTime->getTimestamp();
Now format the output
echo $month = $DateTime->format( 'm' );
echo $day = $DateTime->format( 'd' );
or to get you desired output:
echo $output = $DateTime->format( 'dS F' );
<?php
list($month,$day) = explode(':',date('n:j',strtotime('2012-07-12 17:50:00')),2);
echo 'Month: '.$month.'<br />'."\n";//Month: 7
echo 'Day: '.$day.'<br />'."\n";//Day: 12
?>
And...
<?php
echo date('jS F',strtotime('2012-07-12 17:50:00'));//Outputs: 12th July
?>
Assuming the timestamp is a string, something like this should work if you're looking for a regex solution...
<?php
$string = '2012-07-12 17:50:00';
preg_match_all('/\d{4}-(\d{2})-(\d{2})/', $string, $matches);
$month =$matches[1][0];
$day = $matches[2][0];
?>

How do I use PHP to get the current year?

I want to put a copyright notice in the footer of a web site, but I think it's incredibly tacky for the year to be outdated.
How would I make the year update automatically with PHP 4 or PHP 5?
You can use either date or strftime. In this case I'd say it doesn't matter as a year is a year, no matter what (unless there's a locale that formats the year differently?)
For example:
<?php echo date("Y"); ?>
On a side note, when formatting dates in PHP it matters when you want to format your date in a different locale than your default. If so, you have to use setlocale and strftime. According to the php manual on date:
To format dates in other languages,
you should use the setlocale() and
strftime() functions instead of
date().
From this point of view, I think it would be best to use strftime as much as possible, if you even have a remote possibility of having to localize your application. If that's not an issue, pick the one you like best.
<?php echo date("Y"); ?>
My super lazy version of showing a copyright line, that automatically stays updated:
© <?php
$copyYear = 2008;
$curYear = date('Y');
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Me, Inc.
This year (2008), it will say:
© 2008 Me, Inc.
Next year, it will say:
© 2008-2009 Me, Inc.
and forever stay updated with the current year.
Or (PHP 5.3.0+) a compact way to do it using an anonymous function so you don't have variables leaking out and don't repeat code/constants:
©
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?>
Me, Inc.
With PHP heading in a more object-oriented direction, I'm surprised nobody here has referenced the built-in DateTime class:
$now = new DateTime();
$year = $now->format("Y");
or one-liner with class member access on instantiation (php>=5.4):
$year = (new DateTime)->format("Y");
http://us2.php.net/date
echo date('Y');
strftime("%Y");
I love strftime. It's a great function for grabbing/recombining chunks of dates/times.
Plus it respects locale settings which the date function doesn't do.
This one gives you the local time:
$year = date('Y'); // 2008
And this one UTC:
$year = gmdate('Y'); // 2008
Here's what I do:
<?php echo date("d-m-Y") ?>
below is a bit of explanation of what it does:
d = day
m = month
Y = year
Y will gives you four digit (e.g. 1990) and y for two digit (e.g. 90)
For 4 digit representation:
<?php echo date('Y'); ?>
2 digit representation:
<?php echo date('y'); ?>
Check the php documentation for more info:
https://secure.php.net/manual/en/function.date.php
echo date('Y') gives you current year, and this will update automatically since date() give us the current date.
print date('Y');
For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php
use a PHP function which is just called date().
It takes the current date and then you provide a format to it
and the format is just going to be Y. Capital Y is going to be a four digit year.
<?php echo date("Y"); ?>
<?php echo date("Y"); ?>
This code should do
use a PHP date() function.
and the format is just going to be Y. Capital Y is going to be a four digit year.
<?php echo date("Y"); ?>
$dateYear = date('Y');
echo "Current Year: $dateYear";
Current Year: 2022
$dateYear = date('y');
echo $dateYear;
22
If your server supports Short Tags, or you use PHP 5.4, you can use:
<?=date("Y")?>
Just write:
date("Y") // A full numeric representation of a year, 4 digits
// Examples: 1999 or 2003
Or:
date("y"); // A two digit representation of a year Examples: 99 or 03
And 'echo' this value...
BTW... there are a few proper ways how to display site copyright. Some people have tendency to make things redundant i.e.: Copyright © have both the same meaning. The important copyright parts are:
**Symbol, Year, Author/Owner and Rights statement.**
Using PHP + HTML:
<p id='copyright'>© <?php echo date("Y"); ?> Company Name All Rights Reserved</p>
or
<p id='copyright'>© <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p
For up to php 5.4+
<?php
$current= new \DateTime();
$future = new \DateTime('+ 1 years');
echo $current->format('Y');
//For 4 digit ('Y') for 2 digit ('y')
?>
Or you can use it with one line
$year = (new DateTime)->format("Y");
If you wanna increase or decrease the year another method; add modify line like below.
<?PHP
$now = new DateTime;
$now->modify('-1 years'); //or +1 or +5 years
echo $now->format('Y');
//and here again For 4 digit ('Y') for 2 digit ('y')
?>
To get the current year using PHP’s date function, you can pass in the “Y” format character like so:
//Getting the current year using
//PHP's date function.
$year = date("Y");
echo $year;
The example above will print out the full 4-digit representation of the current year.
If you only want to retrieve the 2-digit format, then you can use the lowercase “y” format character:
$year = date("y");
echo $year;
1
2
$year = date("y");
echo $year;
The snippet above will print out 20 instead of 2020, or 19 instead of 2019, etc.
Get full Year used:
<?php
echo $curr_year = date('Y'); // it will display full year ex. 2017
?>
Or get only two digit of year used like this:
<?php
echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
?>
My way to show the copyright, That keeps on updating automatically
<p class="text-muted credit">Copyright ©
<?php
$copyYear = 2017; // Set your website start date
$curYear = date('Y'); // Keeps the second year updated
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?>
</p>
It will output the results as
copyright # 2017 //if $copyYear is 2017
copyright # 2017-201x //if $copyYear is not equal to Current Year.
best shortcode for this section:
<?= date("Y"); ?>
<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>
You can use this in footer sections to get dynamic copyright year
In Laravel
$date = Carbon::now()->format('Y');
return $date;
In PHP
echo date("Y");
$year = date("Y", strtotime($yourDateVar));
in my case the copyright notice in the footer of a wordpress web site needed updating.
thought simple, but involved a step or more thann anticipated.
Open footer.php in your theme's folder.
Locate copyright text, expected this to be all hard coded but found:
<div id="copyright">
<?php the_field('copyright_disclaimer', 'options'); ?>
</div>
Now we know the year is written somewhere in WordPress admin so locate that to delete the year written text. In WP-Admin, go to Options on the left main admin menu:
Then on next page go to the tab Disclaimers:
and near the top you will find Copyright year:
DELETE the © symbol + year + the empty space following the year, then save your page with Update button at top-right of page.
With text version of year now delete, we can go and add our year that updates automatically with PHP. Go back to chunk of code in STEP 2 found in footer.php and update that to this:
<div id="copyright">
©<?php echo date("Y"); ?> <?php the_field('copyright_disclaimer', 'options'); ?>
</div>
Done! Just need to test to ensure changes have taken effect as expected.
this might not be the same case for many, however we've come across this pattern among quite a number of our client sites and thought it would be best to document here.
create a helper function and call it
getCurrentYear();
function getCurrentYear(){
return now()->year;
}
Print current month with M, day with D and year with Y.
<?php echo date("M D Y"); ?>
For more pricise in second param in date function
strtotime return the timestamp passed by param
// This work when you get time as string
echo date('Y', strtotime("now"));
// Get next years
echo date('Y', strtotime("+1 years"));
//
echo strftime("%Y", strtotime("now"));
With datetime class
echo (new DateTime)->format('Y');

Categories