Get the "Right" Year of a Certain Date with PHP DateTime - php

I was trying to transform a date "YYYY/MM/DD" into "YYYY/WW" format, so I can store the weekly aggregation data, which has a structure below
aggre_date(YYYY/WW) value id
But I found a nasty problem
$dateTime = new DateTime("2014-12-30");
echo $dateTime->format("Y-W")."\n";
$dateTime = new DateTime("2014-01-01");
echo $dateTime->format("Y-W")."\n";
The result is exactly same 2014-01, but the former should be 2015-01.
Is there any way to improve my weekly aggregation data design or to get the "right" year?

You need to use o for the ISO year:
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
$dateTime = new DateTime("2014-12-30");
echo $dateTime->format("o-W")."\n";
$dateTime = new DateTime("2014-01-01");
echo $dateTime->format("o-W")."\n";
2015-01
2014-01
Demo

Related

How to get latest date from now on a given date using carbon in laravel

I want the nearest date passed from given date using carbon
$givenDate = "2020-07-18";
Today is = 2020-12-01
So the result should be the 18th last month. $result = "2020-11-18"
you can use subMonth() from now to get the last month then create a new datetime with suitable values:
$givenDate =Carbon::parse( "2020-12-20");
if($givenDate>Carbon::now())
$result=Carbon::create(Carbon::now()->year,Carbon::now()->month,$givenDate->day)
->format('yy-m-d');
else
$result=Carbon::create(Carbon::now()->year,Carbon::now()->subMonth()->month,$givenDate->day)
->format('yy-m-d');

How to calculate months between two dates including the starting date month?

I'm trying to apply the straight line deprecation for this, I've two date starting date 2018-09-17 (YYYY-MM-DD) ending date 2018-12-30. I need answer in months which is 4 not 3 because, it give me 3.
I've tried starting date 2018-09-01 and ending date 2019-01-01. It give me correct answer 4 what I want.
It give me answer 4
$d1 = new DateTime("2018-09-01");
$d2 = new DateTime("2019-01-01");
echo $d1->diff($d2)->m . " Months";
I need answer 4 for these two date
2018-17-09 to 2018-30-12
Is there any way to get the answer 4 in months including the starting date. I am getting the answer 4 from these date (2018-09-01) to (2019-01-01). In short, I want to include the current date month.
You can use Carbon:
use \Carbon\Carbon;
$from = Carbon::createFromFormat('Y-d-m', '2018-17-09');
$to = Carbon::createFromFormat('Y-d-m', '2018-30-12')->addMonth();
return $to->diffInMonths($from);
Live demo here
You can easily use format method and pass "n" to it this will get the month as a numeric value then you can define your logic.
An example:
$date1 = (new Datetime("2018-09-17"))->format("n");
$date2 = (new Datetime("2018-12-30"))->format("n");
echo $date2 - $date1 + 1;

Week number unique sortable integer id

My question is about programming philosophy, I give an example in PHP language but can be asked in any programming language:
If I have to give an unique sortable integer ID to a day, I can use 'Ymd' format
If I have to give an unique sortable integer ID to a month, I can use 'Ym' format
If I have to give an unique sortable integer ID to a week, I cannot use 'YW' format
Because of 2017-01-01 and 2017-12-31 will return the same week ID:
$date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
$date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
echo $date1->format('YW').' '.$date2->format('YW');
// returns 201752 201752
So I can use the first day ID of the week as unique sortable integer ID week but there is maybe a simpler way to solve it ? A better practice ?
You can use o instead:
echo $date1->format('oW').' '.$date2->format('oW');
//201652 201752
o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
http://php.net/manual/en/function.date.php
You've to be aware when using week numbers with years. There is already a contribution note at php.net for this scenario 6 years back. Have a look here, Hope this will help you understand clearly :)
Reason:
Y is year from the date
o is ISO-8601 year number
W is ISO-8601 week number of year
$date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
$date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
echo $date1->format('YW').' '.$date2->format('YW');
echo PHP_EOL;
echo $date1->format('oW').' '.$date2->format('oW');
DEMO: https://3v4l.org/sMKAF

How to get the result of difference between two date using php? [duplicate]

This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 5 years ago.
I have this php code
$today_date = date('d/m/Y H:i:s');
$Expierdate = '09/06/2017 21:45:03';
$remaindate = date_diff($today_date,$Expierdate);
echo $remaindate;
and i need result from difference between two date.
date_diff() needs a DateTimeInterface as an argument. In other words, you need to create a DateTime object first, using new DateTime() as shown below.
$today_date = new DateTime();
$Expierdate = new DateTime('09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Live demo
The above would output
90 days
Because today is June 8th, and the format 09/06/2017 is September 6th - because you're using American format (MM/DD/YYYY).
If you ment June 9th (tomorrow), you need to use European format (MM-DD-YYYY, note the dash instead of slash). You can alternatively use DateTime::createFromFormat() to create from a set format, so your current format, 09/06/2017, is interpreted as June 9th. The code would then be
$today_date = new DateTime();
$Expierdate = DateTime::createFromFormat('d/m/Y H:i:s', '09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Output (live demo)
1 days
In any case, $remaindate holds some properties which can be used (see the manual), or you can format it to your liking by supplying the desired formation into the format() method.
new DateTime()
DateTime::diff()
DateTime::format()
DateTime::create_from_format()

Reduce 100 years from a given date in php

$date_raw = '05/05/1995';
$newDate = (date('j F Y', strtotime('-192years -14months -2days', strtotime($date_raw))));
print "New Date: $newDate <br>";
I'm trying to subtract 100+ years from a given date. but the value i get is real till 92 years only. after that i don't get correct subtraction. whats the reason?
If you need to work with dates that fall outside the range of a 32-bit signed unix timestamp (1901-12-13 to 2038-01-19), then start using DateTime objects
$date_raw = '05/05/1995';
$newDate = (new DateTime($date_raw))
->sub(new DateInterval('P192Y14M2D'))
->format('j F Y');
echo $newDate, PHP_EOL;
gives
3 March 1802
or you can do this in the following way
// set your date here
$mydate = "2018-06-27";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastHundredyear = strtotime("-100 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastHundredyear);
this will give you following output
1918-06-27

Categories