Wrong date in jdtojewish function in php - php

I want to convert the Gregorian calendar to the Hebrew date, so I use this code:
$jd = gregoriantojd(2, 25, 2020);
echo jdtojewish($jd); // the reuslt is: 5/30/5780
and then, when I add one day to the date, the month jumps up two months instead of one month,
$jd = gregoriantojd(2, 26, 2020);
echo jdtojewish($jd); // the result is: 7/1/5780
The right output should be 6/1/5780. how can I get the right Hebrew date ?

Here is the explanation in 9 year old bug report https://bugs.php.net/bug.php?id=61185
The problem is that on non-leap years PHP function jdtojewish just skips 6th month

Related

Last week of year and 1st January with Carbon?

I'm working on a project and I need to format date in the following : YYYYWW where WW is the week number in the year, for example : today is 202131.
There are several ways to do this, I can use isoFormat('YYYYWW') directly, or I can get weekOfYear attribute and append it to current year.
Both methods however, break for the following date : 01-01-2021, indeed, the 1st january is set in the last week of 2020, but the year is 2021. Both methods give : 202153 while the correct result should be 202053 .
I currently have a workaround by checking if the month is less than 3 and the week is bigger than 50 then there is a problem and I decrement the year by 1.
public static function formatTestWeek($d)
{
$current = Carbon::parse($d);
$currentWeek = $current->weekOfYear;
$currentYear = $current->year;
if($currentWeek > 50 && $current->month < 3){
$currentYear -= 1;
}
$formattedDate = strval($currentYear) . $current->isoFormat("WW");
return $formattedDate;
}
Is there a more elegant approach in Carbon to get YYYYWW format that works in all cases ?
YYYY is the year
GGGG is the ISO week-year
gggg is the week-year following current locale settings (first_day_of_week + day_of_first_week_of_year)
So you need ->isoFormat('GGGGWW')
Complete list of codes available in isoFormat() are in the documentation:
https://carbon.nesbot.com/docs/#iso-format-available-replacements

php date() method is showing wrong date [duplicate]

This question already has an answer here:
strtotime() with only year return wrong data
(1 answer)
Closed 2 years ago.
I have this PHP code:
<?php
$date = 2022;
$str = strtotime(2022);
echo $str; // 1613161320
echo '<hr/>';
echo date('Y', $str); // 2021
why this link date('Y', $str); is showing 2021 instead of 2022 ?
strtotime() expects a formated date string like 10 September 2000 see php docs.
When you want to get a unix timestamp by seperate year, month, day, hour, minute and second values you should have a look at mktime() see php docs.
So what you want to get the first 2022-date possible is:
$str = mktime(0, 0, 0, 1, 1, 2022);

Strange Date Issue

I have a form where users can enter a birthdate. If a date prior to 1970 is entered, e.g. 1964, it is saved in the database as 2064. Can anyone shed some light on what is happening? Thanks. I am formatting the dates for MySQL using the function below:
function formatdate( $s ) {
$date = date_create($s);
return date_format($date, 'y-m-d');
}
MySQL assumes that years less than 70 (when not given as a 4 digit year) are in the 21st century and not the 20th. You should format as a 4 digit year to avoid this problem:
return date_format($date, 'Y-m-d');
Use a capital Y in your format mask to return a 4 digit year.

Can't get previous month from DateTime in PHP- Is this a (pretty big) bug?

I need to create functions in PHP that let me step up/down given datetime units. Specifically, I need to be able to move to the next/previous month from the current one.
I thought I could do this using DateTime::add/sub(P1M). However, when trying to get the previous month, it messes up if the date value = 31- looks like it's actually trying to count back 30 days instead of decrementing the month value!:
$prevMonth = new DateTime('2010-12-31');
Try to decrement the month:
$prevMonth->sub(new DateInterval('P1M')); // = '2010-12-01'
$prevMonth->add(DateInterval::createFromDateString('-1 month')); // = '2010-12-01'
$prevMonth->sub(DateInterval::createFromDateString('+1 month')); // = '2010-12-01'
$prevMonth->add(DateInterval::createFromDateString('previous month')); // = '2010-12-01'
This certainly seems like the wrong behavior. Anyone have any insight?
Thanks-
NOTE: PHP version 5.3.3
(Credit actually belongs to Alex for pointing this out in the comments)
The problem is not a PHP one but a GNU one, as outlined here:
Relative items in date strings
The key here is differentiating between the concept of 'this date last month', which, because months are 'fuzzy units' with different numbers of dates, is impossible to define for a date like Dec 31 (because Nov 31 doesn't exist), and the concept of 'last month, irrespective of date'.
If all we're interested in is the previous month, the only way to gaurantee a proper DateInterval calculation is to reset the date value to the 1st, or some other number that every month will have.
What really strikes me is how undocumented this issue is, in PHP and elsewhere- considering how much date-dependent software it's probably affecting.
Here's a safe way to handle it:
/*
Handles month/year increment calculations in a safe way,
avoiding the pitfall of 'fuzzy' month units.
Returns a DateTime object with incremented month/year values, and a date value == 1.
*/
function incrementDate($startDate, $monthIncrement = 0, $yearIncrement = 0) {
$startingTimeStamp = $startDate->getTimestamp();
// Get the month value of the given date:
$monthString = date('Y-m', $startingTimeStamp);
// Create a date string corresponding to the 1st of the give month,
// making it safe for monthly/yearly calculations:
$safeDateString = "first day of $monthString";
// Increment date by given month/year increments:
$incrementedDateString = "$safeDateString $monthIncrement month $yearIncrement year";
$newTimeStamp = strtotime($incrementedDateString);
$newDate = DateTime::createFromFormat('U', $newTimeStamp);
return $newDate;
}
Easiest way to achieve this in my opinion is using mktime.
Like this:
$date = mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo date('d-m-Y', $date);
Greetz Michael
p.s mktime documentation can be found here: http://nl2.php.net/mktime
You could go old school on it and just use the date and strtotime functions.
$date = '2010-12-31';
$monthOnly = date('Y-m', strtotime($date));
$previousMonth = date('Y-m-d', strtotime($monthOnly . ' -1 month'));
(This maybe should be a comment but it's to long for one)
Here is how it works on windows 7 Apache 2.2.15 with PHP 5.3.3:
<?php $dt = new DateTime('2010-12-31');
$dt->sub(new DateInterval('P1M'));
print $dt->format('Y-m-d').'<br>';
$dt->add(DateInterval::createFromDateString('-1 month'));
print $dt->format('Y-m-d').'<br>';
$dt->sub(DateInterval::createFromDateString('+1 month'));
print $dt->format('Y-m-d').'<br>';
$dt->add(DateInterval::createFromDateString('previous month'));
print $dt->format('Y-m-d').'<br>'; ?>
2010-12-01
2010-11-01
2010-10-01
2010-09-01
So this does seem to confirm it's related to the GNU above.
Note: IMO the code below works as expected.
$dt->sub(new DateInterval('P1M'));
Current month: 12
Last month: 11
Number of Days in 12th month: 31
Number of Days in 11th month: 30
Dec 31st - 31 days = Nov 31st
Nov 31st = Nov 1 + 31 Days = 1st of Dec (30+1)

Find out date of nth week's monday in PHP?

I have a simple situation where I have a user supplied week number X, and I need to find out that week's monday's date (e.g. 12 December). How would I achieve this? I know year and week.
Some code based mainly on previous proposals:
$predefinedYear = 2009;
$predefinedWeeks = 47;
// find first mоnday of the year
$firstMon = strtotime("mon jan {$predefinedYear}");
// calculate how much weeks to add
$weeksOffset = $predefinedWeeks - date('W', $firstMon);
// calculate searched monday
$searchedMon = strtotime("+{$weeksOffset} week " . date('Y-m-d', $firstMon));
An idea to get you started:
take first day of year
add 7 * X days
use strtodate, passing in "last Monday" and the date calculated above.
May need to add one day to the above.
Depending on the way you are calculating week numbers and the start of the week this may sometimes be out. (i.e. if the monday in the first week of the year was actually in the previous year!)
TEST THIS THOROUGHLY - but I've used a similar approach for similar calcualtions in the past.
This will solve the problem for you. It mainly derives from Mihail Dimitrov's answer, but simplifies and condenses this somewhat. It can be a one-line solution if you really want it to be.
function getMondaysDate($year, $week) {
if (!is_numeric($year) || !is_numeric($week)) {
return null;
// or throw Exception, etc.
}
$timestamp = strtotime("+$week weeks Monday January $year");
$prettyDate = date('d M Y');
return $prettyDate;
}
A couple of notes:
As above, strtotime("Monday January $year") will give you the timestamp of the first Monday of the year.
As above +X weeks will increment a specified date by that many weeks.
You can validate this by trying:
date('c',strtotime('Sunday Jan 2018'));
// "2018-01-07T00:00:00+11:00" (or whatever your timezone is)
date('c',strtotime('+1 weeks Sunday Jan 2018'));
// "2018-01-14T00:00:00+11:00" (or whatever your timezone is)
date('c',strtotime('+52 weeks Sunday Jan 2018'));
// "2019-01-06T00:00:00+11:00"
Due to reputation restriction i can't post multiple links
for details check
http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.mktime.php
you can use something like this :
use mktime to get a timestamp of the week : $stamp = mktime(0,0,0,0,<7*x>,) {used something similar a few years back, so i'm not sure it works like this}
and then use $wDay = date('N',$stamp). You now have the day of the week, the timestamp of the monday should be
mktime(0,0,0,0,<7*x>-$wDay+1,) {the 'N' parameter returns 1 for monday, 6 for sunday}
hope this helps
//To calculate 12 th Monday from this Monday(2014-04-07)
$n_monday=12;
$cur_mon=strtotime("next Monday");
for($i=1;$i<=$n_monday;$i++){
echo date('Y-m-d', $cur_mon);
$cur_mon=strtotime(date('Y-m-d', strtotime("next Monday",$cur_mon)));
}
Out Put
2014-04-07
2014-04-14
2014-04-21
2014-04-28
2014-05-05
2014-05-12
2014-05-19
2014-05-26
2014-06-02
2014-06-09
2014-06-16
2014-06-23

Categories