Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to create DateTime interval to compare it with date difference. Is something like this possible?
$week = new \DateTime("1 week");
Well, you can use DateInterval class instead. While you cannot pass the relative timeformats such as '1 week' into its constructor directly, there's a useful static method createFromDateString() that supports such syntax:
$weekInterval = \DateInterval::createFromDateString('1 week');
The problem is that DateInterval instances are not comparable in PHP (check this feature request for details):
$someInterval = (new \DateTime('2015-09-14'))->diff(new \DateTime('2015-09-13'));
var_dump($weekInterval > $someInterval); // false somehow
DateTime instances are comparable, however. So one possible workaround is making a comparison between the results of adding two DateInterval to the same fixed date. For example:
$now = new \DateTimeImmutable();
var_dump($now->add($weekInterval) > $now->add($someInterval)); // true
Eval.in demo.
I think what you are looking for is the diff(); function in the DateTime class.
$dateOne = new DateTime('2015-10-11');
$dateTwo = new DateTime('2014-10-13');
$interval = $dateOne->diff($dateTwo);
echo $interval->format('%R%a days');
Comparing UNIX TimeStamp
$dateTime = new DateTime("#" . $someOtherTimeStamp);
$endTime = new DateTime("#" . time());
$interval = $dateTime->diff($endTime);
Reading Material
diff();
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
can you please help me?
i need to change
2017-12-31 11:45:00
to
2017-12-31T11:45
using php function.
I need this datetime-local format for putting in a input value.
This might do it for you:
<?php
$date = '2017-12-31 11:45:00';
$newdate = date('Y-m-d\TH:i', strtotime($date));
echo $newdate; // 2017-12-31T11:45
;?>
Documentation about strtotime: http://php.net/manual/en/function.strtotime.php
NOTE:
strtotime max date is '19 Jan 2038 03:14:07 UTC'.
New solution: PHP 5 >= 5.2.0, PHP 7
Please consider to use the new DateTime function: http://php.net/manual/en/datetime.construct.php
Example new DateTime:
<?php
$d = new DateTime("9999-12-31");
$d->format("Y-m-d"); // "9999-12-31"
$d = new DateTime("0000-12-31");
$d->format("Y-m-d"); // "0000-12-31"
$d = new DateTime("-9999-12-31");
$d->format("Y-m-d"); // "-9999-12-31"
?>
This can be done quite simply using the DateTime object provided with PHP
<?php
$dt = DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-31 11:45:00');
$newFormat = $dt->format('Y-m-d\TH:i');
echo $newFormat;
Result:
2017-12-19T11:45
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My date string value is 1478025000. want to get year and month in the string format itself from this string,is it possible? the day should be 0 and next month and next year like that.
my desired output should be 1477852200
Try something like this:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY
If you want to get just the year and month as an epoch, try something like:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to echo 3 previous months from a string.
$monthyear = "01/2015";
OUTPUT
12/2014
11/2014
10/2014
Go ahead and try the following:
<?php
// Given string:
$monthyear = "01/2015";
// Init DateTime object:
$datetime = new DateTime();
// Create a new datetime instance, and parse given string:
$date = $datetime->createFromFormat('m/Y', $monthyear);
// Loop for 3 iterations:
for ($i = 1; $i <= 3; $i++) {
// Print current date:
echo $i . ". " . ($date->format('m/Y')) . "<br>";
// Reduce one month:
$date = date_add($date, date_interval_create_from_date_string('-1 months'));
}
?>
Yealds:
1. 01/2015
2. 12/2014
3. 11/2014
You can test this code in phpfiddle.org
Sources:
PHP createFromFormat
PHP date_add
So what you're trying to do here is get a range of dates (3 months ago to now) at a specific interval (1 month). This is easily done with something like PHP's DatePeriod class.
First you need to create a DateTime object from that string, which you can do using something like DateTime::createFromFormat. This DateTime object can be used as your ending range.
$endRange = DateTimeImmutable::createFromFormat("m/Y", $monthyear);
Second, you can subtract 3 months from that DateTime object using DateTime::sub in order to get the starting range. To do this you use a DateInterval object to specify the interval of time you wish to subtract from the DateTime object.
$startRange = $endRange->sub(new DateInterval('P3M'));
Finally, you can create your DatePeriod object using the $startRange and $endRange along with a DateInterval of 1 month and traverse the object to get the 3 desired dates.
$period = new DatePeriod($startRange, new DateInterval('P1M'), $endRange);
foreach($period as $date) {
echo $date->format("m/Y");
}
This should give you the desired dates 10/2014, 11/2014, and 12/2014.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a variable $dob which returns a date, in the following format: 1970-02-01 00:00:00
How can I use php to calculate the age of the person?
Your question is explained in detail in the PHP documentation, it bascially goes like this:
// create a datetime object for a given birthday
$birthday = new DateTime("2012-12-12");
// substract your timestamp from it
$diff = $birthday->diff($dob);
// output the difference in years
echo $diff->format('%Y');
Try this:
<?php
$age = '1970-02-01 00:00:00';
echo (int)((time()-strtotime($age))/31536000);
Output: 43, in years.
$dob = '1970-02-01 00:00:00';
$date = new DateTime($dob);
$diff = $date->diff(new DateTime);
echo $diff->format('%R%a days');
Basically stolen from here: http://uk1.php.net/manual/en/datetime.diff.php
Formatting options: http://uk1.php.net/manual/en/dateinterval.format.php
One-liner:
echo date_create('1970-02-01')->diff(date_create('today'))->y;
Demo.
i would try this
$user_dob = explode('-',$dob);
$current_year= date('Y');
$presons_age = $current_year - $user_dob[0];
This is an untested code but i feel u should get the logic.
strtotime() will convert your date into a timestamp, then subject that from time() and the result is the age in seconds.
$age_in_seconds = time() - strtotime('1970-02-01');
To display the age in years (+- 1 day), then divide by the number of seconds in a year:
echo "Age in whole years is " . floor($age_in_seconds / 60 / 60 / 24 / 365.25);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to convert the date like this
$original_date = date("M/d/Y"); // the output of that is Aug/27/2013
to "08/27/2013"
Like this:
date('m/d/Y', strtotime($original_date));
strtotime can convert pretty much anything reasonable you give it into a Unix timestamp, even stuff like "Next Friday".
EDIT
Funny, but seems that strtime doesn't work with a date formatted like that... The first thing that came to my mind was to replace those slashes with spaces, using str_replace or implode/explode or whatever works for you...
$newDate = date('m/d/Y', strtotime(str_replace('/', ' ', $origDate)));
$newDate = date('m/d/Y', strtotime(implode(' ', explode('/', $origDate))));
Simply by passing it back to the date function
$original_date = date("M/d/Y");
$new_date = date('m/d/Y', strtotime($original_date));
Simply change the format string:
$originalDate = date("m/d/Y"); // = 08/27/2013
Or use DateTime and specify the input format to avoid ambiguity:-
$dateString = \DateTime::createFromFormat('M/d/Y', $origanalDate)->format('m/d/Y');