Quicker way to get date of ISO week from a date string? - php

I'm being passed a date string (most likely in ISO8601 format) and need to convert it to the date of the ISO week to store as a DATETIME column in MySQL. To initialize the DateTime object the I want to save, I'm doing the following:
$date = new DateTime("now");
$date = new DateTime( $date->format("o-\WW") );
echo $date->format(DateTime::ISO8601) . "\n";
Since I'm using Doctrine2, I need to pass my entity a DateTime object. Is there a way to avoid making 2 DateTime objects to get the same result? Should I drop back to the date function and use that as the argument to the DateTime constructor?
$date = new DateTime( date("o-\WW", strtotime("now") );

You could use setISODate to update the first DateTime object using the week and year of the object via format():
$date = new DateTime();
$date->setISODate($date->format('o') , $date->format('W'));
echo $date->format(DateTime::ISO8601);

You could use modify() method of DateTime object.
$date = new DateTime();
$date->modify('sunday this week');
echo $date->format(DateTime::ISO8601) . "\n";
Note that if you want the first day of the week to be something other than Sunday, you will likely need to do something like the following. This example considers Monday as the first day of the week, thus for dates on a Sunday, you would need to get the date of the Monday from the previous week.
$date = new DateTime();
if ($date->format('D') === 'Sun') {
$date->modify('monday last week');
} else {
$date->modify('monday this week');
}
echo $date->format(DateTime::ISO8601) . "\n";

You can probably use date like this:
$date = new DateTime( date('o-\WW') );
Though that formatting looks a bit strange. :p You can of course also use some other method/function the class has to offer to change/modify the date.

Related

PHP: why year not subtracted from date?

The below code doesn't subtract 1 year from the date. Why?
$date1 = '2021-06-02';
$date2 = new \DateTime(date($date1, strtotime('-1 year')));
echo $date2->format('Y-m-d'); // outputs the same date 2021-06-02
Part of your problem is that the date function's first argument is the format of the date.
https://www.php.net/manual/en/function.date.php
So what is happening is that you are creating a date string with the format of '2021-06-02'.
https://www.php.net/manual/en/datetime.format.php
This doesn't use anything from the timestamp that you are providing so this string is passed to the constructor of DateTime and creating the date instead of the one from the year previous.
Please use this code. Its always works for me.
$date1 = '2021-06-02';
$date2 = date("Y-m-d", strtotime("-1 year", strtotime($date1)));
echo $date2; //Output 2020-06-02
This is for Date time object:
$dt = new DateTime('2021-06-02');
$minusOneYearDT = $dt->sub(new DateInterval('P1Y'));
$minusOneYear = $minusOneYearDT->format('Y-m-d');
echo $minusOneYear;
OR make a small solution:
$time = new DateTime('2021-06-02');
$newtime = $time->modify('-1 year')->format('Y-m-d');
echo $newtime;
Your code is a bit of a muddle:
date() takes as parameters a format string, and an integer representing a point in time; it then applies the format to create a string for that date and time
strtotime() takes a string, interprets it as a point in time, and returns an integer timestamp
new DateTime() takes a string, in any of the formats strtotime would accept, but creates an object representation rather than returning an integer
You've tried to use all of them at once, and got in a mess:
Your call to date() has a first parameter of '2021-06-02', which isn't a valid format.
Your call to strtotime() has a parameter of '-1 year', which will just be interpreted as "1 year before now", not relative to anything else you've specified.
Using both of those functions and then passing to new \DateTime() doesn't make a lot of sense, since the object can do all the same things those functions can do.
If you want to use the integer-based functions, you could write this:
$date1 = '2021-06-02';
$date2 = strtotime("$date1 -1 year");
echo date('Y-m-d', $date2);
If you want to use the object-based functions, you could write this:
$date1 = '2021-06-02';
$date2 = new \DateTime("$date1 -1 year");
echo $date2->format('Y-m-d');
Or this (note the use of DateTimeImmutable instead of DateTime to avoid the modify method changing the $date1 object:
$date1 = new \DateTimeImmutable('2021-06-02');
$date2 = $date1->modify('-1 year');
echo $date2->format('Y-m-d');

How to say "next month 15th" in PHP's DateTime?

What I want to do:
Getting a certain day from a certain month directly via the DateTime methods (no mktime stunts :)), like
$day = new DateTime('15th of next month');
but it's not possible to set a fixed day by it's number.
Can anybody help ?
EDIT: I've changed the DateTime from this month to next month to make the problem more clear.
You can use DateTime::createFromFormat(). If you only pass the day value, it'll default to the current month and year.
$date = DateTime::createFromFormat('d', '15');
echo $date->format('Y-m-d'); // 2018-10-15
Edit for the new question requirements:
$date = DateTime::createFromFormat('d', 15)->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); // 2018-11-15

If date has passed in this year, get it from next year

I'm using Carbon for the dates. Let's take 1st of March for example. By default Carbon returns the 1st of March for the current year.
//Returns current year value
$date = Carbon::parse('first day of March');
Is it possible to get it definitely from the future (next year), if it has already passed in this year, without using if conditions.
//Shorten this part
$date = Carbon::parse('first day of March');
if ($date->lessThanOrEqualTo(Carbon::now())) {
$date->addYear();
}
$output = $date->format('d-m-Y');
Carbon is just a wrapper class for PHP's DateTime class.
Therefore, this should work:
<?php
$date = new DateTime('2018-03-01');
$today = new DateTime();
if ($date < $today) {
$date->modify('+1 year');
}
echo $date->format('d/m/Y');
Output: 01/03/2019
See it working here https://3v4l.org/i1HvL
Learn the actual PHP DateTime class here https://secure.php.net/manual/en/class.datetime.php
I have no idea why you wouldn't use if to check IF something. Maybe you'd like a ternary better?
$date = ($date < $today) ? $date->modify('+1 year') : $date;
Maybe not cleaner than your method, but it's different and it's working.
I create an array with all the dates relevant, today, 1 of March, and 1 of March next year and sort them.
Then I find 'today' with array_search in the array and grab the next value.
$date = array(Carbon::parse('today'),Carbon::parse('first day of March'), Carbon::parse('first day of March + 1 year'));
sort($date);
$date = $date[array_search(Carbon::parse('today'), $date)+1];
var_dump($date);
I honestly think this is slower than a if(), but it does not use an if() as OP requested :-)

How i can change current date not all just days?

I want change the current date or i want put variable in current date like this:
$companyDates = $company_dates['dates']; //this variable come from DataBase
$databaseDate=DateTime::createFromFormat("Y-m-d", $companyDates);
$day=$databaseDate->format('d'); // this is how i take just days from date format(y-m-d)
$dt = new DateTime();
$today = $dt->format('Y-m-d');
$oldDate=$dt->format('Y-m-$day'); // here i want days from database into current day month and year.
and then i want to find different day like this:
$date1=date_create($today);
$date2=date_create($oldDate);
$diff=date_diff($date1,$date2);
Is this possible OR is this my way right ?
Try this:Use setDate
$day=10;
$dt = new DateTime();
$today = $dt->format('Y-m-d');
$out = new DateTime();
$out->setDate($out->format('Y'), $out->format('m'), $day);
$oldDate= $out->format('Y-m-d');
$d1=new DateTime($oldDate);$d2=new DateTime($today);
$difference = $d1->diff($d2);
echo $difference->format('%r%a days');
You're trying to pass desired day inside format
$oldDate=$dt->format('Y-m-$day');
It's wrong format (and if you want to concatenate string with variable you should use double quote(") instead of single('))
Use setDate() method instead of.
$today = new DateTime();
$oldDate = clone $today; // Clone instead creating new instance because two different DateTime instances may have different dates
$oldDate->setDate($oldDate->format('Y'), $oldDate->format('m'), $day);

How do I remove 3 months from a date?

Assume the date is:
$date = "2011-08-28";
It need to calculate 3 months previous to $date - how can that be done?
$new_timestamp = strtotime('-3 months', strtotime($date));
You can then use the new timestamp with the php date() function to display the new date how you wish, for example:
echo date("Y-m-d",$new_timestamp);
For me this way is much better because the code is more readable.
$datetime = Datetime::createFromFormat('Y-m-d', "2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');
edit: I'm an idiot. You could do the above as follows
$datetime = new Datetime("2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');
edit: As pointed out by calumbrodie, you can use the sub method instead of inverting the interval and adding it to the date object
I was trying to do something similar to the original question. I needed to subtract 1 day from a DateTime object. I know the other solutions work, but here's another way I liked better. I used this function:
function getPreviousDay($dateObject){
$interval = new DateInterval('P1D');
$dateObject->sub($interval);
return $dateObject;
}
$dateObject is a DateTime object that can have any date you wan't, but as I wanted the current date, I wrote:
$dateObject = new DateTime('now');
What my function does, is subtract 1 day from the date it receives, but you can modify it so it subtracts 3 months modifying the DateInterval constructor like this:
$interval = new DateInterval('P3M');
$dateObject->sub($interval);
return $dateObject;
You can find the explanation on the string format used in the DateInterval constructor here
DateInterval constructor documentation
There you'll se that letter 'P' (period) is mandatory, the you use an int to specify period length and then specify the period type, in the case of months 'M'. It looks as if there was an error in the documentation, it says that "M" is used for months and minutes, but it actually works for months. If you need to use minutes, you must specify "PTM", for example "PT3M" for 3 minutes, outside the table it says you must use the "T" identifier for time intervals.
edit:
To give a complete example, you have to use this format for a full date time interval:
$format = 'P1Y2M4DT2H1M40S';
This will represent an interval of 1 year, 2 months, 4 days, 2 hours,1 minute and 40 seconds
Hope this helps someone
<?php
$threemonthsago = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
?>
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>

Categories