I have a problem comparing a customized php datetime with current datetime, the code is below:
$requestDate = new DateTime("2014-05-28 11:14:00");
$nowDate = new DateTime();
$aklTimeZone = new DateTimeZone("Pacific/Auckland");
$requestDate->setTimezone($aklTimeZone);
$nowDate->setTimezone($aklTimeZone);
echo $nowDate->format("Y-m-d H:i:s");
if ($requestDate > $nowDate) {
echo '1';
} else if($requestDate == $nowDate){
echo '0';
} else{
echo "-1";
}
The printed result is:
2014-05-28 12:21:31
1
The $requestDate is supposed to be less than the $nowDate, how come it is greater here?
Thank you
Related
I am beginner webdeveloper.
I use in my project Laravel 5.8.
I have this this code:
if ($this->calcutateAge($request->input('date')) < 18) {
return Redirect::back()->withErrors(['You are a minor. Registration is allowed for adult users']);
}
function calcutateAge($dob)
{
$dob = date("Y-m-d", strtotime($dob));
$dobObject = new DateTime($dob);
$nowObject = new DateTime();
$diff = $dobObject->diff($nowObject);
return $diff->y;
}
It's work fine. But I have problem with date ex 2045-12-12.
This function is not working. With year: 2015-12-12 - it's okey.
How can I repair it?
I suggest you should use Carbon.
use Carbon\Carbon;
function calcutateAge($dob){
return \Carbon::parse($dob)->age;
}
I would write your function to return true or false depending on if the user is 18 or not.
function is_18($dob)
{
$dobObject = new DateTime(date("Y-m-d", strtotime($dob)));
$nowObject = new DateTime();
return $dobObject < $nowObject ? ($dobObject->diff($nowObject)->y > 18) : false;
}
Then your IF block is simplified to this:
if (!$this->is_18($request->input('date')) {
return Redirect::back()->withErrors(['You are a minor. Registration is allowed for adult users']);
}
I would find out when a person born 18 years ago was born, then compare to that:
function isAdult($dob) {
$adult = new DateTime('18 years ago'); // 'date' => '2002-09-01 12:05:52.000000'
$dob = date("Y-m-d", strtotime($dob));
$dobObject = new DateTime($dob);
return $adult >= $dobObject; // 2002-09-01 is after the passed date of birth
}
you can add 18 year and compare the date like this:
$dobObject = new DateTime($dob.' +18 years');
$nowObject = new DateTime();
if ($dobObject > $nowObject)
print 'great you are granted'
else
print 'sorry you are minor'
If you run var_dump($diff); you will see:
["invert"]=> int(1)
Which means is a negative difference.
So, you can do return $diff->invert? -$diff->y : $diff->y;
Of course this is a solution based on your code. You can always check if the date is someday into the future, and return false or throw some exception in this case.
I have a datetime, obtained by getting current time and formatting it to my user's timezone. Let's say it's "18", for 6pm in 24 hour format.
I need to have a true/false returned when checking if it falls between 15h one day, and 10h the next day. Between 3pm and the next 10am.
My code so far looks like this, but I cant get PHP to understand that the end-hour is in the next day. What am I doing wrong?!
$deviceTZ = 'America/Toronto';
$dt = new DateTime("now", new DateTimeZone($deviceTZ));
$dt->setTimestamp(time());
$deviceTime = $dt->format('H');
$silentStart = 15;
$silentEnd = 10;
$silentStartDT = date_create_from_format('H', $silentStart);
$silentEndDT = date_create_from_format('H', $silentEnd);
if($silentStartDT > $silentEndDT)
{ $silentEndDT->modify('+1 day'); }
if (($deviceTime > $silentStartDT) && ($deviceTime < $silentEndDT))
{ echo "Silent Period"; }
else { echo "Not Silent"; }
I think I may have come up with a solution
$deviceTime = new DateTime("now", new DateTimeZone($deviceTZ));
$silentStartDT = new DateTime($silentStart.":00");
$silentEndDT = (new DateTime($silentEnd.":00"))->modify('+1 day');
if($silentStart == 0 && $silentEnd == 0)
{ echo "No Silent Mode set."; }
else{
if ($deviceTime >= $silentStartDT && $deviceTime <= $silentEndDT)
{ echo "Within Range<br>"; }
}
Please, i need to get the time difference between a particular time and the current time. ie. I want to track users who submit an assignment late. the deadline time is 10:00AM everyday.I have surf,but all the solution i am seeing does not seems to work. see below:
<?php
$d1 = new DateTime(date('Y-m-d 10:00:00'));//Deadline time
$d2 = new DateTime(date('Y-m-d H:i:s'));//Submission time
$interval = $d1->diff($d2);
$c = $interval->minute;
if($c>0){
echo "submitted late";}else{ echo "Submitted on time";}
?>
Regarding the first point, your code doesn't work because $interval->minute is always positive. So you should simply compare two dates.
Use the following code to achieve it:
$d1 = new \DateTime(date('Y-m-d 10:00:00'));//Deadline time
$d2 = new \DateTime(date('Y-m-d H:i:s'));//Submission time
if($d1 < $d2) {
echo "submitted late";
} else {
echo "Submitted on time";
}
$deadline = date('10:00:00');
$now = date('H:i:s');
if($now > $deadline){
echo "submitted late";
} else {
echo "Submitted on time";
}
as simple as that
So I'd like to add the current time to the database, to a specific user when he does something, and later on read it, and check if that time has passed (by checking current time and substracting that from the one in database; to check if it has passed or not)
So how would I do this? I tried with something like this:
$date = date("YmWjis");
$calculate = $date - $info['lastvisit'];
if($calculate <= -1)
{
echo "you need to wait before visiting again"; // (just an example)
} else {
//do something
}
I also tried both:
!$calculate < 0
$calculate < 0
etc. But I can't get it to work. Can anyone help me? :P
edit for Parag;
$date = date("YmWjis");
$dote = date("YmWjis") + $time; // ($time is set earlier and is 30 seconds)
echo "wait " . $date = $date - $dote . " seconds until next visit";
work?
It says like "wait 20138269786674 seconds until next visit".
You can try something like this:
$dateDiff = new DateTime("2014-04-27 22:00:15");
$date = new DateTime();
$diff = $date->diff($dateDiff);
if($diff->invert == 0)
{
echo "you need to wait before visiting again"; // (just an example)
} else {
//do something
}
$db_time = "2014/04/28 15:15:15";
$cur_time = "2014/04/28 18:15:15";
if(strtotime($cur_time) > strtotime($db_time))
{
// Current time exceeds DB time
$diff = date('Y/m/d H:i:s', strtotime($cur_time)-strtotime($db_time));
echo $diff;
}
else
{
// Current time didn't exceeds DB time
}
UPDATE
$date = strtotime(date("YmWjis"));
$dote = strtotime(date("YmWjis")) + $time; // ($time is set earlier and is 30 seconds)
echo "wait " . $date = $date - $dote . " seconds until next visit";
DEMO
http://3v4l.org/LBIXu
Don't use a database. This does the job without the db overhead. It uses PHP sessions.
<?php
session_start();
if (!isset($_SESSION['lastVisitTime'])) {
$_SESSION['lastVisitTime']=new DateTime();
} else {
$now=new DateTime();
if ($_SESSION['lastVisitTime']->diff($now) > $someMaxValueYouDefine) {
echo "You must wait before visiting again.";
}
}
I currently have a registration form which checks a persons date of birth via three inputs
day(dd) - month(mm) - year(yyyy)
The code I am using to check:
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
return $end_date - $start_date;
}
//Enter date of birth below in MM/DD/YYYY
$dob="$day/$month/$year";
$dob2 = "$dob";
$one = round(dateDiff("/", date("d/m/Y", time()), $dob2)/365, 0) . " years.";
if($one <13){
?>
You must be 13 years of age or older to join!
<?
}else{
?>
YAY you're 13 or above!
<? } ?>
I am receiving an error saying:
error: Warning: gregoriantojd() expects paramater 1 to be long string
Can anyone help me with this?
Thank you in advance!
Why complicate when you can do it in really simple way:
function dateDiff($dateFormat, $beginDate, $endDate)
{
$begin = DateTime::createFromFormat($dateFormat, $beginDate);
$end = DateTime::createFromFormat($dateFormat, $endDate);
$interval = $begin->diff($end);
if($interval->y >= 13)
{
echo 'Over 13';
}
else
{
echo 'Not 13';
}
}