Trying to do a simple echo statement if prior to date except I'm only getting the else echo ...
<?php
if (time() < strftime('2016-01-13 00:00:00'))
{echo "You're early!";}
else {
echo "You're late!";}
?>
Little help. Thanks. Yes, I'm new to this.
Your requirement is to convert date time stamp into unix timestamp (seconds since 01-01-1970).
You can use strtotime() but, we have another accurate method for this.
Use DateTime().
<?php
$date = new DateTime('2016-01-13 00:00:00');
$timeStamp = $date->getTimestamp();
if (time() < $timeStamp) {
echo "You're early!";
}
else {
echo "You're late!";
}
?>
Related
I need to find If $now(00:00:00) if included from $a and $b
echo $a= date('H:i:s', strtotime("23:00:00"));
echo $now = date('H:i:s', time("now");*(es.00:00:00)*
echo $b=date('H:i:s', strtotime("01:00:00"));
if ($now>$a && $now<$b) {echo "si";} else {echo "no";}
output say: no
__________________EDIT____________________________
Ok thanks to all, I think give you more information:
I read with php an file .xml, from this file, I get some information about program in tv, with :
-the name of program, -name of channel, -and time to program start in this format (20151210004500 +0100)
I think at this point is more easy to use this format, or not?
I would find in array from .xml the program on air, for make this I must compared the date and time in this format, It is possible?
Can you help me?
Sorry for my poor english, thanks.
Apply strtotime before comparation
if(strtotime($now) > strtotime($a) && strtotime($now) < strtotime($b)
{
// do your staff
}
$now = new DateTime;
$a = (new DateTime)->setTime(23, 0, 0);
if ($now > $a && $now < $a->modify('+2 hours')) {
echo 'si';
} else {
echo 'no';
}
You can easily do by using time() function to get the current time in numeric format (seconds from the 1970-1-1).
Than you can convert your dates into the same time format with strtotime() function.
$now = time();
$a = strtotime("10 september 2010");
$b = strtotime("10 september 2020");
if( $now > $a && $now < $b )
echo "Yes";
else
echo "No";
I created two datetime objects where $date1 = 09/02/2013 and $date2 = 03/02/2014
When I run the following code:
if ($date2 < $date1)
{
echo "hi";
}
for some reason it echos "hi" although $date2 is clearly greater than $date1. How am i supposed to compare these two dates? Please help!
<?php
$date1 = new DateTime ('2013-12-25');
$date2 = new DateTime ('2014-11-24');
if ($date1 > $date2) {
echo ('date1 is greater than date2');
}
else {
echo ('date2 is greater than date1');
}
use like below with function http://php.net/manual/en/function.strtotime.php
if (strtotime($date2 ) < strtotime($date1))
{
echo "hi";
}
hope this will sure help you.
That could work in JavaScript, but in PHP it will not :P
However, you could calculate an interval between dates.
$interval = $date1->diff($date2);
if ($interval->invert){ //1 if negative and 0 if positive
// $date2 has a bigger time value
} else {
// $date1 has a bigger time value
}
I am trying to check if a certain time is in the past. Solution 1 works but the solutions 2 and 3 using strtotime do not. Any ideas why the strtotime solutions fail with this date when they work fine when the date is not that distant (f.ex. using 27.05.2035 works)?
<?php
$date = "27.05.2045";
$hour = "22";
$min = "15";
// 1. This one works
$datetime = DateTime::createFromFormat('d.m.Y H:i', $date.' '.$hour.':'.$min);
$now = new DateTime();
if ($datetime < $now)
{
echo "Datetime is in the past";
}
else if ($datetime > $now)
{
echo "Datetime is in the future";
}
// 2. Does not work
if (time() > strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime)";
}
else if (time() < strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime)";
}
// 3. Using another date format but still does not work
$array = explode('.', $date);
$date_converted = $array[2].'-'.$array[1].'-'.$array[0];
if (time() > strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime with converted date)";
}
else if (time() < strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime with converted date)";
}
?>
The 32 bit integer maximum makes it impossible to represent dates past 19 January 2038.
The solution would be to either:
Use DateTime objects, which do not represent dates using the number of seconds passed since 1970, but using a field for each time unit.
Use the 64-bit version of PHP, where the maximum integer is much higher.
See The 2038 Year Problem for more details.
I have this code:
$curdate = '22-02-2011';
$mydate = '10-10-2011';
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This would echo expired BUT shouldn't because $mydate is in the future and therefore smaller than the $curdate but PHP is looking at JUST the first two numbers 22 and 10 instead of the whole string. How can I fix this?
Thanks
Try converting them both to timestamps first, and then compare two converted value:
$curdate=strtotime('22-02-2011');
$mydate=strtotime('10-10-2011');
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This converts them to the number of seconds since January 1, 1970, so your comparison should work.
The problem is that your current variables are strings, and not time variables.
Try this out:
$curdate = strtotime('22-02-2011');
$mydate = strtotime('10-10-2011');
$row_date = strtotime($the_date);
$today = strtotime(date('Y-m-d'));
if($row_date >= $today){
-----
}
$currentDate = date('Y-m-d');
$currentDate = date('Y-m-d', strtotime($currentDate));
$startDate = date('Y-m-d', strtotime("01/09/2019"));
$endDate = date('Y-m-d', strtotime("01/10/2022"));
if (($currentDate >= $startDate) && ($currentDate <= $endDate)) {
echo "Current date is between two dates";
} else {
echo "Current date is not between two dates";
}
Use the PHP date/time classes to convert these string representations into something you can directly compare using getTimestamp() to compare the UNIX times.
If you're sure all your dates are in this format, you can string slice them into YYYY-MM-DD, and a string comparison will function correctly then.
if(strtotime($curdate) > strtotime($mydate))
{
...
}
it's VERY simple
$curdate = '2011-02-22';
$mydate = '2011-10-10';
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
I have a date like this: 16/02/2011
What I want to do is have a simple PHP conditional that checks if todays date is either that DAY or AFTER so for example:
<?php
$mydate = '26/01/2010';
if($mydate == date('dd/mm/yyyy')
{
echo 'last day to reply';
}
elseif($mydate == 'date after todays date')
{
echo 'post has expired and you cannot reply';
}
else
{
echo 'post has NOT expired and you can reply';
}
?>
So if today's date is 01/01/2011 then it would say post has not expired
if date is 17/02/2011 then it would say last day to reply
and if the date is after 25/02/2011 then it would say it has expired.
Can anyone help? Thanks :)
The easiest way would be to use mktime to convert the required date and times (the last date and the ultimate deadline) and then directly compare the current time against the those.
the easiest way is to have a date already in the proper format. Especially if it's coming from database.
<?php
$mydate = '2010-01-26';
$curdate = date('Y-m-d');
if($curdate == $mydate)
{
echo 'last day to reply';
}
elseif($curdate > $mydate)
{
echo 'post has expired and you cannot reply';
}
else
{
echo 'post has NOT expired and you can reply';
}
?>
note that by reading this code you will have no problem understanding what does it do.
it's almost natural language and self-explanatory
unlike all other codes here.
Rather than compare visual dates it's probably better to compare timestamps:
$deadline = strtotime('2010-01-26');
$today = mktime(0, 0, 0);
if ($today == $deadline) {
echo 'last day';
}
else if ($today > $deadline) {
echo 'past day';
}
else {
echo 'a-okay';
}
In simplistic terms, you'd want to do somethinn like:
if (strtotime($date) > date()) {
echo "Your post has expired";
}
it's very difficult to compare dates-that-are-strings, especially when you consider that "2/3/4" could be any of 8 different dates (Feb 3rd, '04; Mar 4th, '02; etc..). Keeping dates/times as actual timestamp values in PHP makes the comparisons far easier.
You can use mktime (or strotime if you can get your end date in a proper format, 17-02-2011 or 02/17/2011):
$mydate = '17/02/2011';
$mydate_parts = explode('/', $mydate);
$mydate_timestamp = mktime(0, 0, 0, $mydate_parts[1], $mydate_parts[0], $mydate_parts[2]);
if($mydate == date('d/m/Y'))
{
echo 'last day to reply';
}
elseif($mydate_timestamp < time())
{
echo 'post has expired and you cannot reply';
}
else
{
echo 'post has NOT expired and you can reply';
}
I would strongly recommend you look up on the DateTime class documentation; if you're not interested though; a string slice would work.
Something along the lines of the following is probably the quickest for you.
if( mktime(0,0,0,substr($mydate, 3, 2), substr($mydate, 0, 2), substr($mydate, 6, 4) ) > time() )