How to get a specific date in the current year - php

Suppose the current year is 2014; then I want get the date 2014-06-30, if the current year is 2015 then I want 2015-06-30
I tried date('Y'); but it is just giving current year.

You can just use:
date("Y-06-30")

Demo : https://eval.in/103028
try:
echo date("Y-06-30");
OUTPUT:
2014-06-30
See date documentation:
http://php.net/manual/en/function.date.php

You can concatenate the rest of your required date to your date() function as follow
echo date('Y') . '-06-30';
//output 2014-06-30

Use this :
date("Y-06-30");
Y will be the your year and rest will same.
Where y is A full numeric representation of a year, 4 digits for more Info : Date()

You need to put like below code:
$d = date('Y');
$data = $d."-06-30";
echo $data;

Related

Ranking the current day in PHP automatically

Attempt:
The following code prints the current day of the respective date.
<?php
$current_date = date("D");
echo $current_date;
?>
Output:
The above code gives the following output as:
(As of date: 6/19/2018)
Tue
Required Output:
How can I rank the day automatically as:
Sun1
Mon2
Tue3
Wed4
Thu5
Fri6
Sat7
Required Output Example:
Instead of getting the output as Tue only. What I want the output is Tue3 or similar type.
Suppose today is Tuesday as of (6/19/2018). Then I want the output as:
Tue3
Suggestions are highly appreciated.
http://php.net/manual/en/function.date.php
w = Numeric representation of the day of the week (0 Sun, 6 Sat).
D = A textual representation of a day, three letters.
+1 the w, to give you the right number, then append it.
<?php
$rank = date("w")+1;
echo date("D").$rank;
Result:
Tue3
Hello You can archive by this way
echo date('D').(date('w')+1);
Please refer for more info http://php.net/manual/en/function.date.php

Finding the number of the day in a week in PHP from a date ( d-m-Y ) format

I have a variable $date='15-06-2013' . Now, how do I get the number of the day in the week? 15-06-2013 is a Saturday and hence my function should return 6 as the number if I were to use N format character
Use this code
echo $day_of_week = date('N', strtotime('15-06-2013'));
Output
6
Codepad
Alternatively, using the DateTime class:-
echo (new \DateTime('15-06-2013'))->format('N');
Output:-
6
See it working

Passing a UK date in a URL

I am passing a date in a URL in a UK format as per the following:
http://www.website.com/index.php?from_date=01/04/2013&to_date=12/04/2013
The date range is 1st April 2013 to 12th April 2013.
Then I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$rpt_from_date = date("Y-m-d", strtotime($_GET["from_date"]) );
$rpt_to_date = date("Y-m-d", strtotime($_GET["to_date"]) );
echo $rpt_from_date;
echo "</br>";
echo $rpt_to_date;
?>
For some reason this is not working. The above returns the following:
2013-01-04
2013-12-04
It's switching the month and day around. I want it to return:
2013-04-01
2013-04-12
Does anyone have any ideas?
Use DateTime object, to get php understand in which format you passing date to it.
$rpt_from_date = DateTime::createFromFormat('d/m/Y', $_GET["from_date"]);
echo $rpt_from_date->format('Y-m-d');
PHP is reading your time string in the US format (MM/DD/YYYY) because you are using slashes. You could use dashes to give the time: index.php?from_date=01-04-2013&to_date=12-04-2013, or convert it yourself:
$uktime = implode("-", explode("/", $_GET['from_date']));
I will provide the solution but it is not using the date function:
$arr = explode("/",$_GET["from_date"]);
$from_date = $arr[2]."-".$arr[1]."-"$arr[0];
Second solution is as following:
$from_date = implode(array_reverse(explode("/",$_GET["from_date"])));

how can i get which month a given year day is in in php?

I have been given year day (1-366) and I need to figure out which month it is in, how can I do this?
Well, I actually have a date string like : year, day or year, minute of day, second and I ultimately want to create a POSIX timestamp from it, how can I do this?
Thank you!
If you have PHP >= 5.3, then you can use DateTime::createFromFormat.
$day = 176;
$date = DateTime::createFromFormat('z', $day);
echo $date->getTimestamp(); // 1372275280
<?php
$year=2013;
$d=360;
echo date("m",strtotime("1/1/$year + $d days"))
?>
Use the date function to get a posix time stamp.
To get the month of a certain date, use intval(date('m'), mktime($h,$m,$s,$month,$day,$year))

PHP: date function to get month of the current date

I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?
Thanks,
Jonesy
I used date_format($date, "m"); //01, 02..12
This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01 just becomes 1
See http://php.net/date
date('m') or date('n') or date('F') ...
Update
m Numeric representation of a month, with leading zeros 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
F Alphabetic representation of a month January through December
....see the docs link for even more options.
What does your "data variable" look like? If it's like this:
$mydate = "2010-05-12 13:57:01";
You can simply do:
$month = date("m",strtotime($mydate));
For more information, take a look at date and strtotime.
EDIT:
To compare with an int, just do a date_format($date,"n"); which will give you the month without leading zero.
Alternatively, try one of these:
if((int)$month == 1)...
if(abs($month) == 1)...
Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.
$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime);
echo date('y', $unixtime );
as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php ) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so
echo date('n'); // "9"
As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.
<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";
// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);
// Output it month is various formats according to http://php.net/date
echo date('M',$dateAsUnixTimestamp);
// Will output Apr
echo date('n',$dateAsUnixTimestamp);
// Will output 4
echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>
To compare with an int do this:
<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
echo "They are the same";
}
?>

Categories