Strange Date Issue - php

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.

Related

Wrong date in jdtojewish function in 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

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

Adding leading zeroes to a string date in PHP

I have a string "date" which can be DD.MM.YYYY or D.M.YYYY (with or without leading zeros), it depends what a user types.
Then I use it in a condition to send another email when the day is today.
if($_POST["date"]== date("d.m.Y")){
$headers.="Bcc: another#mail.cz\r\n";
}
The problem is that the mail is send when the date format DD.MM.YYYY (with leading zeros) only.
My proposed solution
As I'm not very good in PHP, I only know the solution theoretically but not how to write the code - I would spend a week trying to figure it out on my own.
What's in my mind is dividing the date into three parts (day, month, year), then checking the first two parts if there's just one digit and adding leading zeros if it's the case. I don't know how to implement that to the condition above, though. I have read a few topics about how to do this, but they were a bit more different than my case is.
You should equalize to same format d.m.Y and you can do this with strtotime and date function:
$post_date = date("d.m.Y", strtotime($_POST["date"]));
if($post_date == date("d.m.Y")){
$headers.="Bcc: another#mail.cz\r\n";
}
I changed date to $post_date for more clear. I'll try to explain difference with outputs
echo $_POST["date"]; // lets say: 8.7.2013
echo date("d.m.Y"); // 09.09.2013 > it's current day
strtotime($_POST["date"]); // 1373230800 > it's given date with unix time
$post_date = date("d.m.Y", strtotime($_POST["date"])); // 08.07.2013 > it's given date as right format
If you use date function without param, it returns as current date.
Otherwise if you use with param like date('d.m.Y', strtotime('given_date'));, it returns as given date.
$post_date = date("d.m.Y", strtotime($_POST["date"]));
At first, we converted your date string to unix with strtotime then equalized and converted format that you used in if clause.
first set date format with leading Zero
$postdate = strtotime('DD.MM.YY', $_POST['date']);
and also matching date will be in same format
$matching_date = date('DD.MM.YY', strtotime('whatever the date'));
then
if ( $postdate === $matching_date )
{
// send mail
}
Why don't you just check the length of the _POST (it can be either 8 or 10)
if (strlen($_POST["date"]) == 10) {
$headers.="Bcc: another#mail.cz\r\n";
}

PHP convert 2 digit year to a 4 digit year

I have data coming from the database in a 2 digit year format 13 I am looking to convert this to 2013 I tried the following code below...
$result = '13';
$year = date("Y", strtotime($result));
But it returned 1969
How can I fix this?
$dt = DateTime::createFromFormat('y', '13');
echo $dt->format('Y'); // output: 2013
69 will result in 2069. 70 will result in 1970. If you're ok with such a rule then leave as is, otherwise, prepend your own century data according to your own rule.
One important piece of information you haven't included is: how do you think a 2-digit year should be converted to a 4-digit year?
For example, I'm guessing you believe 01/01/13 is in 2013. What about 01/01/23? Is that 2023? Or 1923? Or even 1623?
Most implementations will choose a 100-year period and assume the 2-digits refer to a year within that period.
Simplest example: year is in range 2000-2099.
// $shortyear is guaranteed to be in range 00-99
$year = 2000 + $shortyear;
What if we want a different range?
$baseyear = 1963; // range is 1963-2062
// this is, of course, years of Doctor Who!
$shortyear = 81;
$year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
Try it out. This uses the modulo function (the bit with %) to calculate the offset from your base year.
$result = '13';
$year = '20'.$result;
if($year > date('Y')) {
$year = $year - 100;
}
//80 will be changed to 1980
//12 -> 2012
Use the DateTime class, especially DateTime::createFromFormat(), for this:
$result = '13';
// parsing the year as year in YY format
$dt = DateTime::createFromFormat('y', $result);
// echo it in YYYY format
echo $dt->format('Y');
The issue is with strtotime. Try the same thing with strtotime("now").
Simply prepend (add to the front) the string "20" manually:
$result = '13';
$year = "20".$result;
echo $year; //returns 2013
This might be dumbest, but a quick fix would be:
$result = '13';
$result = '1/1/20' . $result;
$year = date("Y", strtotime($result)); // Returns 2013
Or you can use something like this:
date_create_from_format('y', $result);
You can create a date object given a format with date_create_from_format()
http://www.php.net/manual/en/datetime.createfromformat.php
$year = date_create_from_format('y', $result);
echo $year->format('Y')
I'm just a newbie hack and I know this code is quite long. I stumbled across your question when I was looking for a solution to my problem. I'm entering data into an HTML form (too lazy to type the 4 digit year) and then writing to a DB and I (for reasons I won't bore you with) want to store the date in a 4 digit year format. Just the reverse of your issue.
The form returns $date (I know I shouldn't use that word but I did) as 01/01/01. I determine the current year ($yn) and compare it. No matter what year entered is if the date is this century it will become 20XX. But if it's less than 100 (this century) like 89 it will come out 1989. And it will continue to work in the future as the year changes. Always good for 100 years. Hope this helps you.
// break $date into two strings
$datebegin = substr($date, 0,6);
$dateend = substr($date, 6,2);
// get last two digits of current year
$yn=date("y");
// determine century
if ($dateend > $yn && $dateend < 100)
{
$year2=19;
}
elseif ($dateend <= $yn)
{
$year2=20;
}
// bring both strings back into one
$date = $datebegin . $year2 . $dateend;
I had similar issues importing excel (CSV) DOB fields, with antiquated n.american style date format with 2 digit year. I needed to write proper yyyy-mm-dd to the db. while not perfect, this is what I did:
//$col contains the old date stamp with 2 digit year such as 2/10/66 or 5/18/00
$yr = \DateTime::createFromFormat('m/d/y', $col)->format('Y');
if ($yr > date('Y')) $yr = $yr - 100;
$md = \DateTime::createFromFormat('m/d/y', $col)->format('m-d');
$col = $yr . "-" . $md;
//$col now contains a new date stamp, 1966-2-10, or 2000-5-18 resp.
If you are certain the year is always 20 something then the first answer works, otherwise, there is really no way to do what is being asked period. You have no idea if the year is past, current or future century.
Granted, there is not enough information in the question to determine if these dates are always <= now, but even then, you would not know if 01 was 1901 or 2001. Its just not possible.
None of us will live past 2099, so you can effectively use this piece of code for 77 years.
This will print 19-10-2022 instead of 19-10-22.
$date1 = date('d-m-20y h:i:s');

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

Categories