date manipulation PHP - php

I need to create a date object that will be changed by parameters that i get.
If I'll get -7 for days it will take me a week ago.
Here is my code. How do I format the date correctly?
public function get_time_get($myear=0,$mmonth=0,$mday=0,$mhour=0,$mminute=0,$msecond=0){
$year=date('y') +$myear;
$month=date('m')+$mmonth;
$day = date('d')+$mday;
$hour= date('H'+$mhour); // there is a bug
$minute = date('i')+$mminute;
$seconds= date('s')+$msecond;
$date=mktime($year,$month,$day,$hour,$minute,$seconds);
$t =date("Y-m-d H:i:s", $date);
debug($date);
}
You can see that I try to get the time , but I get this:2021-11-30 17:08:29
That is not correct

You wrote:
$hour= date('H'+$mhour);
But it should be:
$hour = date('H') + $mhour;
The $mhour should be outside of the date function.

Related

PHP Change date variable after x days

I want to change date var when x days have passed
For instance:
Today is 21.12.16 - $date = '23.12.16'
Tomorrow is 22.12.16 - $date = '23.12.16'
When it's 23.12.16 - $date = '25.12.16'
Her's the code I got so far. Hope this will make some sense
$date = "2016-12-21"; //** will describe this lower
$days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
$new_date = date('d.m.y', strtotime("+2 days"));
} else{
$new_date = $date;
}
This works ok if I just want to do it once
**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php
P.S. sorry for my bad English.
Here's what I came up with:
$date = '2016-12-01'; //Your script start date, you wont need to change this anymore
$everyxdate = 10; // once x days to add x to $date
$days_passed = date_create()->diff(date_create($date))->days; // passed days from start of script $date
$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate; // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want
Hope this will help some one who has the same task I had.

Create Date object in PHP for dates before 1970 in certain format

I have date witch is formatted d-m-y like 22-10-49 I want to convert it to Date object to save it in my mysql Database. I try :
DateTime::createFromFormat('d-m-y', $mydate , new DateTimeZone('GMT'));
but the result is 2049-10-22 instead of 1949-10-22. I searched and got that createFromFormat only returns date after 1970. but I don't know what to do.
P.s: 22-10-49 is what I have, and there are several other dates like this, I cannot change the format of it or convert it to 22-10-1949 or any other formats.
P.S 2 : "I'm working with birthdays and excpect 22-10-15 to be 1915 rather than 2015.
Try this function.
Edit: First we will convert two digit year in 4 digit. Then we will form complete date and pass it to function.
$original_date = '22-10-49';
$date_part = explode('-',$original_date);
$baseyear = 1900; // range is 1900-2062
$shortyear = $date_part[2];
$year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
$subdate = substr( $original_date, 0, strrpos( $original_date, '-' ) );
$string = $subdate."-".$year;
echo safe_strtotime($string);
function safe_strtotime($string)
{
if(!preg_match("/\d{4}/", $string, $match)) return null; //year must be in YYYY form
$year = intval($match[0]);//converting the year to integer
if($year >= 1970) return date("Y-m-d", strtotime($string));//the year is after 1970 - no problems even for Windows
if(stristr(PHP_OS, "WIN") && !stristr(PHP_OS, "DARWIN")) //OS seems to be Windows, not Unix nor Mac
{
$diff = 1975 - $year;//calculating the difference between 1975 and the year
$new_year = $year + $diff;//year + diff = new_year will be for sure > 1970
$new_date = date("Y-m-d", strtotime(str_replace($year, $new_year, $string)));//replacing the year with the new_year, try strtotime, rendering the date
return str_replace($new_year, $year, $new_date);//returning the date with the correct year
}
return date("Y-m-d", strtotime($string));//do normal strtotime
}
Output: 1949-10-22
Source: Using strtotime for dates before 1970

PHP get time from personal identity number under 1970

I have problem, I can't get time from personal identity number under 1970, I need to solve that, but using time. My function looks like. I don't know which way I can go. Thanks!
function getBirthDayFromRd($rd){
$day = substr($rd,4,2);
$month = substr($rd, 2,2);
$year = substr($rd, 0,2);
if($month>=51 and $month<=62){
$month = $month - 50;
}
$time = strtotime($day.".".$month.".".$year);
return date("d.m.Y", $time);
}
strtotime() fails due to its being tied to the Unix epoch which does not support dates prior to 1970. Just use DateTime which can handle pre-1970 dates and convert dates easily:
function getBirthDayFromRd($rd){
$date = DateTime::createFromFormat('ymd',$rd);
if($date->format('Y') > date("Y")) {
$date->modify('-100 years');
}
return $date->format('d.m.Y');
}
DateTime::createFromFormat() parses your date and creates the DateTime object. Then we just call DateTime::format() to format it in the desired format.
update
Just fixed a bug where pre-1970 dates were shown 100 years in the future.
Demo
I solve it another way, but u started me up.
if($year < 70){
$year = $year+1900;
$time = date_create_from_format("d.m.Y", $day.".".$month.".".$year);
return date_format($time, "d.m.Y");
}else{
$time = strtotime($day.".".$month.".".$year);
return date("d.m.Y", $time);
}

calculating a date and returning the week number depending on the start date in php?

im trying to create a function that takes a $start_date as parameter and it should calculate which week number we are in i.e
for instance $start_date equals 2012-08-16
private function get_week_number($start_date){
// get current date
// caluculate the difference between start_date and current_date
// determine which week we are in 1, 2 or 3
// return $week
}
how can i implement this in php? thanks
function get_week_number($start_date){
$date = date('U', strtotime($start_date));
$now = date('U');
return ceil(($now-$date)/60/60/24/7);
}
echo get_week_number('2012-08-16');
I think what you need is merely date_diff.
private function get_week_number($start_date){
return date('W') - date('W', strtotime($start_date));
}

How to convert date into same format?

I want to get the $registratiedag and count a couple of days extra, but I always get stuck on the fact that it needs to be a UNIX timestamp? I did some google-ing, but I really don't get it.
I hope someone can help me figure this out. This is what I got so far.
$registratiedag = $oUser['UserRegisterDate'];
$today = strtotime('$registratiedag + 6 days');
echo $today;
echo $registratiedag;
echo date('Y-m-d', $today);
There's obviously something wrong with the strtotime('$registratiedag + 6 days'); part, because I always get 1970-01-01
You probably want this:
// Store as a timestamp
$registratiedag = strtotime($oUser['UserRegisterDate']);
$new_date = strtotime('+6 days', $registratiedag);
// You'll need to format for printing $new_date
echo date('Y-m-d', $new_date);
// I think you want to compare $new_date against
// today's date. I'd recommend a string comparison here,
// As time() includes the time as well
// time() is implied as the second argument to date,
// But we'll put it anyways just to be clearer
if( date('Y-m-d', $new_date) == date('Y-m-d', time()) ) {
// The dates are equal, do something here
}
else if($new_date < time()) {
// if the new date is earlier than today
}
// etc.
First it converts $registratiedag to a timestamp, then it adds 6 days
EDIT: You probably should change $today to something less misleading like $modified_date or something
try:
$today = strtorime($registratiedag);
$today += 86400 * 6; // seconds in 1 day * 6 days
at least one of your problems is that PHP does not expand variables in single quotes.
$today = strtotime("$registratiedag + 6 days");
//use double quotes and not single quotes when embedding a php variable in a string
If you want to include the value of variable $registratiedag right into the text passed as parameter of strtotime, you have to enclose that parameter with ", not with '.

Categories