I'm writing a code which need to calculate the number of weekdays from a date to today.
I just have today's date and with that i want to pass a number to the function so it can return me back the number of weekdays since x days.
e.g :
function getWorkingDays($number){
// code...
return $value;
}
// if we are monday
getWorkingDays(2) // return Thursday's date
I got this problem since two days now and i'm getting very boring, hope someone got a solution.
This is very easy to do with the DateTime object:
$date = date_create('2020-01-06'); //a Monday
$numberWeekdays = 2;
$date->modify('-'.$numberWeekdays.' weekdays');
echo $date->format('l Y-m-d');
//Thursday 2020-01-02
If you need Today as the basis , you can also use date_create('Today').
I got the following format of date from an API:
1468102548
I'm trying to convert this date to a normal human readable format, however I can't get it to work.
Here's what I've tried so far:
$creation_date = "1468102548";
$input1 = $creation_date / 1000;
$newDate_creation_date = date("Y-m-d", $input1); // Output: 1970-01-17 (It's not right)
That timestamp is not in milliseconds so dividing it by 1000 is skewing your date.
$creation_date = new DateTime('#1468102548');
echo $creation_date->format('Y-m-d'); // Outputs 2016-07-09
Demo
$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
I'm required to take a date in the format 'Y-z' which is year-doy (e.g, 2013-146) and convert that into a unix time stamp to be store into a database.
The issue i have is that i input 2013-146 and turn it into a DateTime Object. then when i output this date in unix or 'Y-m-d' format i get 2013-5-27 not 2013-5-26 which is the correct day.
You can verify the DOY on this NASA website and this NOAA website.
Summary:
--I have the date: '2013-146'
--Using DateTime::createFromFormat and echoing using 'Y-m-d' and 'Y-z' i get: 2013-5-27 and 2013-146 respectively.
--This does not agree with the NASA website I listed and is offset by one day can anyone verify that I'm not losing my mind?
Here is the code you can test:
<?php
date_default_timezone_set('America/Chicago');
$year = 2013; //where this outputs a simple year 'CCYY'
$day = 146; //where this provides the day of year
$format = 'Y-z'; //specifying what format i'm creating the datetime with
$date = $year.'-'.$day; //formatting the strings to the above $format
$timezone = new DateTimeZone('America/Chicago'); //specify the timezone
$fileDateStore = DateTime::createFromFormat($format, $date, $timezone);//, $timezone); //create the DateTime object
$fileDateString = date_format($fileDateStore,"Y-m-d"); //format it so strtotime() can read it
$fileDate = strtotime($fileDateString); //finally create the Unix Timestamp for the date.
$newfileDOY = date_format($fileDateStore,"Y-z");
echo 'newfileDOY = '.$newfileDOY.', ';
echo 'date = '.$date.', ';
echo 'fileDateString = '.$fileDateString.', ';
echo 'fileDate = '.$fileDate.PHP_EOL;
?>
The problem is than z format in PHP begins with 0 and not with 1.
Look at: http://www.php.net/manual/en/function.date.php
z: The day of the year (starting from 0)
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');