Conditional tag with DateInterval - php

how to use conditional tags using DateInterval with hour for scale in PHP
for example
i will add text "active" if at 21:00 until 23:00
<?php
$Hr = date('H:i');
if($hr == 21:00 - 23:00) {
echo "active";
}
else {
echo "not active";
}

Try this, use DateTime to compare time.
$time1 = new DateTime('21:00');
$time2 = new DateTime('23:00');
$interval = $time1->diff($time2);
//echo $interval;
$diff = $interval->format('%H').":".$interval->format('%I');
$Hr = date('H:i');
echo $diff." == ".$Hr."\n";
if($hr == $diff) {
echo "active";
}
else {
echo "not active";
}
DEMO

$Hr = date('H:i');
if(strtotime($hr)>strtotime(21:00) && strtotime($hr)<strtotime(23:00) ){
echo "active";
}else{
echo "not active";
}
Please try this this may work.

Related

How to get birthday date in form of today, tomorrow, yesterday?

I want to get birthday dates in form of 'Today' ,'Tomorrow','Yesterday' form.
1.if candidate birthday was on 26-july-1991 it should be print 'yesterday'
2.if candidate birthday is on 27-july-1991 it should be print 'today'.
3.if candidate birthday will on 28-july-1991 it should be print 'tomorrow'.
code
$current = strtotime(date("Y-m-d"));
$date = strtotime("2014-07-24");
$datediff = $date - $current;
$difference = floor($datediff/(60*60*24*365));
if($difference==0)
{
echo 'today';
}
else if($difference > 1)
{
echo 'Future Date';
}
else if($difference > 0)
{
echo 'tomarrow';
}
else if($difference < -1)
{
echo 'Long Back';
}
else
{
echo 'yesterday';
}
Maybe a bit complicated solution, but here I compare month num and date num:
$current_month = date("n");
$current_day = date("j");
// date of birth
$dob = strtotime("1991-07-26");
$dob_month = date("n", $dob);
$dob_day = date("j", $dob);
if ($current_month == $dob_month) {
if ($current_day == $dob_day) {
echo 'TODAY';
} elseif ($current_day == $dob_day + 1) {
echo 'YESTERDAY';
} elseif($current_day == $dob_day - 1) {
echo 'TOMORROW';
} else {
echo 'IN this month';
}
} elseif ($current_month < $dob_month) {
echo 'In future';
} else {
echo 'Long back';
}
Use the php Date and Time class
Something like this:
$today=new DateTime("2017-07-27");
$other_day=new DateTime("2017-07-28");
$check = $today->diff($other_day);
$difference = (integer)$check->format( "%R%a" );
echo $difference;
just delete * 365 and your code should work

How to find out the number of days between two dates

i am trying to get number of days between two given dates, but while trying this way its not giving the number of days.
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
its not working, not giving the number of days between given two dates.
Try this. It is very simple.
<?php
$date1 = strtotime("2015-11-16 10:01:13");
$date2 = strtotime("2015-05-06 09:47:16");
$datediff = $date1 - $date2;
echo floor($datediff/(60*60*24))." days"; //output 194 days
?>
It's better using DateTime class, you can see comment(9) at PHP manual as it answer your question
Try this,
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$datediff = $pur_dt - $todate;
$diff = $datediff/(60*60*24);
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try This :
$pur_dt = Date('2015-08-03');
$todate = Date(date('Y-m-d'));
$pur_dt = strtotime($pur_dt);
$todate = strtotime($todate);
$seconds_diff = $todate - $pur_dt;
$$diff = floor($seconds_diff/(60*60*24));
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try this
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff->days>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}

I would like to show messages according to time range between two time stamps

My goal to display a message if time range is between given range If not display another one.
I tried;
date_default_timezone_set("Europe/Istanbul");
$saat = date("h:i");
if ($saat <='08:00' && $saat >='22:00') {
echo ('yes we are open');
}
else ('sorry we are closed');{
}
I know i make mistake while trying to get that if time is between the range, but i cannot overcome problem.
waiting for your responses.
Try the following.
$saat = new DateTime();
$open = new DateTime( $saat->format('Y-m-d').' 08:00',new DateTimeZone('Europe/Istanbul'));
$close = new DateTime($saat->format('Y-m-d').' 22:00',new DateTimeZone('Europe/Istanbul'));
if (($saat >= $open) && ($saat <= $close)) {
echo 'yes we are open';
}else{
echo 'sorry we are closed';
}
Try this
date_default_timezone_set("Europe/Istanbul");
$saat = date("h:i");
if ($saat <='08:00' && $saat >='22:00')
{
echo 'yes we are open';
}
else
{
echo 'sorry we are closed';
}
Its better to perform a less than / greater than operation on a DateTime object. Also I think you have your >= and <= confused, and you have an extra bracket.
I changed the name of the $saat variable to $now to make it more understandable.
date_default_timezone_set("Europe/Istanbul");
//Create a DateTime Object represent the date and time right now
$now = new DateTime();
//Today's Opening Date and Time
$open = new DateTime( $now->format('Y-m-d').' 08:00' );
//Today's Closing Date and Time
$close = new DateTime( $now->format('Y-m-d').' 22:00' );
if ($now >= $open && $now <= $close) {
echo ('yes we are open');
}
else ('sorry we are closed');{
}
On a side note, I NEVER use date() because of the 2038 problem (google it). DateTime is not subject to such problems.
If you don't care about the minutes you can do it like this
date_default_timezone_set("Europe/Istanbul");
$saat = date("h");
if ($saat<=8 && $saat>=22) {
echo ('yes we are open');
} else {
echo('sorry we are closed');
}
you can use your condition like
if (strtotime($saat) <=strtotime('08:00') && strtotime($saat) >=strtotime('22:00'))
Ok here is the code :
date_default_timezone_set("Asia/Karachi");
$t=time();
//echo(date("H:i",$t)); // Current Time
$hour = (date("H",$t)); // Current Hour
$minute = (date("i",$t)); //Current Minute
if (($hour <= 8)&&($hour >= 22)) {
echo "We are open";
}
else {
echo "sorry we are closed";
}

We're open / We're closed

<?php
$time = date('Hi');
if ($time > 0030) {
echo "closed";
} elseif ($time >= 0800) {
echo "open";
}
?>
This is the code i'm using for a client's website. In short it's a code that'll show if the client's business is open or not.
My client's working hours are between 8am to 12:30am.
I was wondering if there's an easier way of doing this or am I doing this right?
Try this:
<?php
date_default_timezone_set('Asia/Calcutta'); # set timezone according to your requirement
$now = new DateTime();
$start = new DateTime('today 08:00:00');
$end = new DateTime('tomorrow 00:30:00');
if ($start <= $now && $now < $end) {
echo "open";
}
else{
echo "close";
}
?>

How to make a check if this timestamp is today,tomorrow or the day after tomorrow?

I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.
I have e.g. :
$timestamp = "1313000474";
How to make a check if this timestamp is today,tomorrow or the day after tomorrow?
e.g.
if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";
How to do this?
EDIT: corrected unix timestamp
Thank you in advance.
$timestamp = "1313000474";
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
if ($date == $today) {
echo "today";
} else if ($date == $tomorrow) {
echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Keep your code clean...
$timestamp = "1313000474";
// Description demonstrate proposes only...
$compare_dates = array(
'today' => 'today!!',
'tomorrow' => 'Tomorrow!!!',
'tomorrow +1 day' => 'day after tomorrow? YEAH',
);
foreach($compare_dates => $compare_date => $compare_date_desc){
if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
echo $compare_date_desc;
break;
}
}
EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);
<?php
$time = "20060713174545";
$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
echo "It's today";
}
elseif($date == $tomorrow){
echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
echo "It's day after tomorrow";
}
else{
echo "None of previous if statements passed";
}
<?php
function getTheDay($date)
{
$curr_date=strtotime(date("Y-m-d H:i:s"));
$the_date=strtotime($date);
$diff=floor(($curr_date-$the_date)/(60*60*24));
switch($diff)
{
case 0:
return "Today";
break;
case 1:
return "Yesterday";
break;
default:
return $diff." Days ago";
}
}
?>

Categories