I am new to PHP and would like to compare a specific time with the current time. I have tried different tricks but nothing works for me.
Eg.
$my_time = '2:00';
$active='';
//echo "The time is " . date("h:ia");die;
if (date('h:i') > date('h:i', strtotime($myTime))) {
$active='Dinner';
$dinner=1;
}
else
{
$active='Lunch';
$lunch=1;
}
You have variable definition
$my_time = '2:00'
but you're using
strtotime($myTime))
This might help you
$my_time ="14:08:10";
if (time() >= strtotime($my_time)) {
echo "Dinner";
}else echo "Lunch";
This will compare given time with current time
try this one .
$my_time ="14:08:10";
if (time() >= strtotime($my_time))
{
echo "Dinner";
}
else
{
echo "Lunch";
}
if this doesnt work try to add date in to time.
because adding date will make time to be more specific.
Related
I had visited some of the post retrieving the date difference between 2 dates in SO but it doesn't gave me the answer I was seeking. Same for reading the documentation, I had problem understanding how it works.
I have tried coding it but it doesn't behave like what I was expecting. Here is my code:
<?php
$currentDate = new DateTime();
$createDateJoin = date_create($getDate['date_joined']);
$dateJoin = date_format($createDateJoin, "Y-m-d");
$yearDifference = $currentDate->diff($createDateJoin);
if ($yearDifference->d < 31 && $yearDifference->m = 0 && $yearDifference->y == 0) {
echo $yearDifference->d . " days";
} else if ($yearDifference->m > 3) {
echo $yearDifference->m . " month";
} else if ($yearDifference->y > 1) {
echo $yearDifference->y . " years";
} else {
echo "Not yet assigned";
}
?>
As you can see from my code above, I am trying to do a print when after calculating the difference between the 2 dates, it meets the condition of $yearDifference->.The behavior from the program that I have experienced does not print out the things I want accordingly (E.g Staff working more than 1 year will print out how many years they have work, months for those who just came in and new staff less than a month will print out days).
I would like to know how does ->d/m/y works and how can I actually make use of the d,m and y to draw out the specific date correctly. And I also noticed that when I treat $yearDifference as a String or int, it comes out different result for the conditions. So what should I treat the type to be to manipulate it more easily? Greatly appreciate the help.
You can use this code to get the date different method diff() object returns more values to check you can print_r your object that will print all data member that are returned via diff() method
<?php
echo get_date_diff(strtotime('1990-10-12'),strtotime('2015-10-14'));
function get_date_diff($date,$dateEnd) {
$dStart = new DateTime(date("Y-m-d", $date));
$dEnd = new DateTime(date("Y-m-d", $dateEnd));
$dDiff = $dStart->diff($dEnd);
return($dDiff->y.' years <br>'.$dDiff->m.' months <br>'.$dDiff->d. ' days');
}
?>
I have two dates here and want to compare with another date ...
another date here is called $date_main and compared it with a variable called $date_to ... Help it's not working..
<?php
$date_f="02-05-2014";
$date_t="02-11-2014";
$date_from=date('d-m-Y', strtotime($date_f));
$date_to=date('d-m-Y', strtotime($date_t));
$date_c="21-7-2014";
$date_main=date('d-m-Y', strtotime($date_c));
if(($date_main<$date_to))
{
echo "date_main is less then to<br/>";
}
?>
You'll have to compare the times and not the dates itself:
if(strtotime($date_c) < strtotime($date_t))
I'm wondering why not doing something like this?
$date_f = "02-05-2014";
$date_t = "02-11-2014";
$date_from = DateTime::createFromFormat('d-m-Y', $date_f);
$date_to = DateTime::createFromFormat('d-m-Y', $date_t);
$date_main=DateTime::createFromFormat('d-m-Y', '21-07-2014');
if (($date_main < $date_to)) {
echo "date_main is less then to<br/>";
}
Working with objects is always better.
$date_f="02-05-2014";
$date_t="02-11-2014";
$date_main=date_create("2014-7-21");
$date_to=date_create("2014-11-02");
$diff=date_diff($date_main,$date_to);
if($diff->format("%R%a")>0)
{
echo "date_to is greater";
}
else
{
echo "date_main is greater";
}
I am comparing a deadline time from my database which is on a 24-hour format (like 3:00 = 15:00) to the time now.
For example, if a student wasn't able to submit on the due time, an upload link will be greyed out.
I'm okay with the date comparison, my problem is the time.
Here's my current code:
$date = date("Y-m-d");
$time = strtotime(date('G:i:s'));
$deadtime = strtotime($r['upload_deadtime']);
/*------$deadtime = 15:00:00 ---------*/
if(date($r['upload_deadline']) >= $date && $deadtime > $time){
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
Update: let's just forget about the date comparison, what I'm trying to say is how can I compare my deadline time (which is on a 24-hour format (like 3:00:00 = 15:00:00)) to the time now.
Here's my current code:
$time = strtotime(date('G:i:s')); /*which returns 23:15:42 instead of 14:15:42 */
$deadtime = strtotime('15:00:00');
if($deadtime > $time){
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
date_default_timezone_set('Asia/Manila');
$deadtime = strtotime("15:00:00");
if ($deadtime - time() > 0){
echo '<td>upload</td>';
}else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
This may solve your time comparison issue!!
strtotime("15:00:00") will convert time into unix timestamp of today's 15:00:00 and time() have current unix timestamp.
You just have the comparison backwards on your time check.
You are checking if the deadline date is in the future (assuming the script always runs in the present) and then you are checking that the deadline time is less than the current time (IE before), on a date time epoch value.
With the check you are doing on the strtotime, you do not need to check the dates, as they are already included with the date and time by means of epoch.
Simply do:
if($deadtime > $time){
echo '<td>upload</td>';
} else {
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
This will only show the upload link in the table if the deadline time is in the future.
EDIT:
In the case of your new code, you just need to make the deadtime a strtotime of the deadline date and time stamp from your dBase. Then the code will work.
You are still also comparing the wrong way around!
Example:
/* deadtime = strtotime of datetimestamp in format yyyy-mm-dd G:i:s */
/* $time = strtotime of now. */
if($deadtime > $time){
echo '<td>upload</td>';
} else {
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
Use time PHP's DateTime::Diff
// Current Date and Time
$date_time_now = new DateTime();
// Date Time values from DB (Assuming it's coming from database)
$date_time_deadline = new DateTime('2014-09-08 15:00:00');
// Calculate difference
$difference = $date_time_now->diff($date_time_deadline);
// Output difference in human-readable format
echo "Difference: " . $difference->format('%d days, %h hours, %i minutes, %s seconds');
http://php.net/manual/en/datetime.diff.php
Here's my working code. I think you'll know that my point is.
date_default_timezone_set('Asia/Manila');
$time = date('H:i:s');
$deadline_time = date("h:i a",strtotime($r['upload_deadtime']));
list($hours,$mins,$secs) = explode(":",$time);
$hours = date("H") + 9;/*-gives me wrong hours so i got to modify it-*/
$time_array = array($hours,$mins,$secs);
$new_time =implode(":",$time_array);
$deadtime = strtotime($deadline_time);
$time_now = strtotime($new_time);
if($r['upload_deadline'] < $date && $deadtime < $time_now){/*-compares the date and time-*/
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
So I'm trying to make a message appear on one specific date of the year.
My code now:
<?php
$year = date("Y");
if(checkdate(5, 22, $year) === TRUE) {
echo '<b>something</b>';
}else {
echo '';
}
?>
But the message appears howsoever, no matter the date.
Hope you can help me,
Thanks in advance!
If you don't have to use checkdate, this is an alternative.
<?php
if (date('Y-m-d') == date('Y').'-05-22') {
echo '<b>something</b>';
} else {
echo '';
}
?>
According to the documentation checkdate only validates dates, it doesn't compare them against the current date.
Returns TRUE if the date given is valid; otherwise returns FALSE.
Use something similar to the code you already have to check the date
if( date("n j") == "5 22")
{
echo '<b>something</b>';
}
I am doing an assignment for school, and I get bonus marks if I add a discount on a certain day. I have an actively updating time, using the date function(is it a function?) but I don't know how I would go about making an if statement for this.
Here is my code:
<?php
$t = time();
echo "Date of Purchase: " . (date("Y-m-d", $t));
if(date == "20140224") {
echo $b;
echo "It works!";
}
?>
Obviously I only provided relevant code. Any help is appreciated!
Use DateTime() as DateTime objects are comparable:
$today = new DateTime();
$discountDate = new DateTime('2014-02-24');
if ($today == $discountDate) {
// today is discount day!
}
Tip: No need to use time() to pass a second parameter to date(). date() always assumes "now" when no second parameter is passed
This is untested, but the idea is that you have a function with parameters.
function getPrice($price, $search_date, $happyDays=5) {
if(date('w', strtotime($search_date)) == $happyDays) {
return $price * 0.8; // 20% discount
} else {
return $price; // full price
}
}
Call it like so:
echo "Sum: " . getPrice(100, $_SESSION['purchase_date']);
Ps. I use a session date value, since I think the customer should get the discount if he/she places the order just before midnight :-) (as long as the session is still alive).