I am having issues getting my code to return the correct response.
$Birthd = '06-27-1996';
$NewISSdate = date("m-d-Y", strtotime(date("m-d-Y", strtotime($Birthd)) . " +21 years"));
When I run this the response is: "12-31-1969"
I believe this is a default date of sorts, but what can I do to repair my code? If ran with a different $Birthd string such as "07-03-1996".
You need to change string date format and then add years to it like below:-
$Birthd = '06-27-1996';
$Birthd = str_replace("-","/",$Birthd);
echo $NewISSdate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($Birthd)) . " + 21 years"));
Output:-https://eval.in/835509 OR https://eval.in/835555
Reference:- php different acceptable date formats
Change - to / and try...
$Birthd = '06/27/1996';
$NewISSdate = date("m/d/Y", strtotime(date("m/d/Y", strtotime($Birthd)) . " +21 years"));
Related
I'm having trouble calculating the number of hours worked.
We start with a time which starts as a string in this case ($time).
Then we change the time to 00:00:00 and store the result as a new variable ($newtime).
Then we need to calculate the difference between $time and $newtime but there is a formatting issue which I do not fully understand. Would anyone help?
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - $newtime)/3600, 2);
echo "Hours Worked: " . $worktime . "<br>";
You're subtracting a timestamp with a DateTime object, so it tries to convert the DateTime object to an int, which it can't. You need to get the timestamp for the DateTime object, to subtract two ints:
<?php
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - $newtime->getTimestamp())/3600, 2); // notice the $newtime->getTimestamp() call
echo "Hours Worked: " . $worktime . "<br>";
Demo
DateTime::getTimestamp() reference
You are mixing types (trying to cast object to int)... And maybe you didn't realize about the error you are making because you have disabled errors.
Please use, the method that Datetime class brings to you:
http://php.net/manual/es/datetime.gettimestamp.php
You can do it in both ways:
$newtime->getTimestamp()
or by using this:
date_timestamp_get($newtime)
As this:
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - date_timestamp_get($newtime))/3600, 2);
echo "Hours Worked: " . $worktime . "<br>";
Please, be free of using this: http://sandbox.onlinephpfunctions.com/code/f78c993f709a67ac2770d78bb809e68e3a679707
I would like my output to be - 05/12/2016 <--- year depends on the current year.
$date='05/12/2014';
$current_year='date("Y")';
I've tried combine like below but clearly its wrong. Please help me, how do I combine these two to get my desire output?
$date =(date("d/m", strtotime($date'])) / date("Y"));
$date='05/12/2014';
echo date("d/m", strtotime($date)) ."/". date("Y");
You should try something like this. Concate your / and remove ] .You need to format your database date.It should work.Try:
$databaseDate = date("d/m/Y", strtotime("31/12/2014")); //you need to format your databse Date
$mergeDate = date("d/m", strtotime($databaseTime)) . '/' . date("Y");
echo $mergeDate;
LIVE DEMO
Your approach can work. Just adapt it a bit:
date("d/m", strtotime($date)) . '/' . date("Y");
EDIT:
As pointed out by others, with this type of date format (d/m/Y) you might have issues when date is '25/12/2014'. To be sure your code works in any case use date_parse_from_format to control your date format:
$date = '25/12/2014';
$date_array = date_parse_from_format('d/m/Y',$date);
echo $date_array['day'] . '/' . $date_array['month'] . '/' . date("Y");
When I insert it into my database, it works :
$time = strtotime('1' . '/' . 10 . '/' . '2015');
$res = date('y-n-d',$time);
BUT when I insert it into my databse, it doesn't :
$time = strtotime('10' . '/' . 10 . '/' . '2015');
$res = date('y-n-d',$time);
This code insert in my database the 1970-01-01 default date...
The only change is the length of the day. I tried to change the format of the date, like Y-m-d, but it doesn't change anything.
Use DateTime interface -
$time = new DateTime('10' . '/' . 10 . '/' . '2015');
echo $res = $time->format('Y-m-d');
Use it in following way...
$time = date("d-m-Y", strtotime('1/10/2015'));
$res = date("Y-m-d", $time);
OR
$date = date_create('1/10/2015');
$res = date_format($date, 'Y-m-d');
It looks like you were trying to insert a timestamp (integer) into a DATE field. If your db is MySQL and your using DATETIME type, then you could use this php date format:
date("Y-m-d H:i:s")
Also, I'd suggest using new DateTime() as #sgtBose mentioned, as you might get unexpected results from strtotime() in different locales.
Try this:
$unixDate='01/01/1900';$differenceValue = strtotime($time)- strtotime($unixDate);
insert $differenceValue to Database.
I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015
Hi I know this question is a bit trivial. I googled it but could not find the exact problem anywhere.
I have a string which contains a application filling date how can I convert this string into date
$appln_filling_date = '20020315';
The type of appln_filling_date is string I want to convert its type to date and want the data remain the same. The field in the database has a type date.
EDIT
This is what I am trying to do
$appln_filling_date = strtotime($appln_data['bibliographic-data']['application-reference']['document-id']['1']['date']['$']);
$appln_filling_date = date('Y-m-d', $appln_filling_date);
If your date is in some standard date format use this example:
$d = new DateTime('20020315');
echo $d->format('Y-m-d');
# or [if you do not have DateTime, which is in php >= 5.2.0]
$t = strtotime('20020315');
echo date('Y-m-d', $t);
If your date is not in some standard format use this example:
$d = DateTime::createFromFormat('d . Y - m', '15 . 2002 - 03');
echo $d->format('Y-m-d');
$date = DateTime::createFromFormat('Ymd', $appln_filling_date);
This worked for me like a charm
$appln_filling_date = $appln_data['bibliographic-data']['application-reference']['document-id']['1']['date']['$'];
$appln_filling_date = date_create(date('Y-m-d', $appln_filling_date));
Thanks for the down voting