<?php
date_default_timezone_set('America/Los_Angeles');
$urlYear = '2013';
$currentYear = date('Y');
$systemDate = date('d-m-Y');
if ( ($systemDate >= '01-06-'.$currentYear) || ($currentYear < $urlYear) ) {
echo 'Here are your results <br>';
echo 'System Date '.$systemDate.' '.$currentYear.' '.$urlYear;
} else {
echo 'No results for you';
}
?>
What I'm trying to achieve is:
(If the current date as formatted by me is >= '01-06'-year of the current year)
OR
(if the $currentYear < $urlYear)
//echo 'Here are your results';
But I seem to be getting true for everything. Can you pls help?
You can compare date after converting it to unix timestamp using strtotime,
if ( (strtotime($systemDate) >= strtotime('01-06-'.$currentYear)) || ($currentYear < $urlYear) ) {
echo 'Here are your results <br>';
echo 'System Date '.$systemDate.' '.$currentYear.' '.$urlYear;
} else {
echo 'No results for you';
}
DEMO.
Format your date strings YYYYmmdd when you compare, it will act like an incremental INT. With that, comparing would be easier.
$systemDate = date('Ymd');
Hope this helps.
Why don't you create DateTime objects with the dates you have and compare these
$UrlYear = new DateTime('2013' . '-01-01');
$CurrentYear = new DateTime(date('Y') . '-01-01');
$SystemDate = new DateTime();
if ( ($SystemDate >= $CurrentYear) || ($CurrentYear < $UrlYear) ) {
echo 'Here are your results <br>';
echo 'System Date '. $SystemDate->format('d/m/Y') .' '. $CurrentYear->format('Y') .' '. $UrlYear->format('Y');
} else {
echo 'No results for you';
}
compare date time using timestamp format, use mktime function to convert your date time to timestamp format
You are comparing strings lexically. This doesn't work with formatted date strings.
Transform everything to unix timestamps (UTC) and compare numbers or use date_diff functions
You can use date for comparison but it should be in Y-m-d format:
$systemDate = date( 'Y-m-d' );
$targetDate = date( 'Y-m-d', mktime( 0, 0, 0, 6, 1 ) );
if ( $systemDate >= $targetDate ) {
}
Related
I get the accurate result if i set the start date and end date when the year is 2017. But when the Start date in 15-October-2016 to 15-September-2017 it is not working and show the error message-
$error = "End date can't be older than begin date ";
Here is my code
$data['beginDate'] = $this->input->post('beginDate');
$data['endDate'] = $this->input->post('endDate');
if($data['endDate'] < $data['beginDate']){
$error = "End date can't be older than begin date ";
$this->session->set_flashdata('error', $error);
redirect('/search');
}
else{
$this->load->model('personhistory');
$data['rets'] = $this->personhistory->searchDetails();
$data['sum'] = $this->personhistory->searchDetailsSum();
$data['title'] = "Search Details";
$this->load->view('search_results', $data);
}
Is there any solution to get rid of this problem? Please Help...
PHP does not know what your strings are, and if the values are coming from posted data, then they are strings:
// These are strings
$s1 = '15-October-2016';
$s2 = '15-September-2016';
Instead, you need to create datetime objects for your comparison
$d1 = new DateTime('15-October-2016');
$d2 = new DateTime('15-September-2016');
if( $d1 < $d2 )
echo 'First date comes before the second date';
if( $d1 > $d2 )
echo 'First date comes after the second date';
if( $d1 == $d2 )
echo 'First date and second date are the same';
Try this code and alter the dates, and you will see I am right.
UPDATE:
Technically you could also use strtotime
<?php
$s1 = strtotime('15-October-2016');
$s2 = strtotime('15-September-2016');
if( $s1 < $s2 )
echo 'First date comes before the second date';
if( $s1 > $s2 )
echo 'First date comes after the second date';
if( $s1 == $s2 )
echo 'First date and second date are the same';
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 in the form MM-YYYY (e.g: 04-2000). I exploded the date into month and year and am now trying to check certain conditions on the month:
between 1 and 12
formed of 2 digits)
and on the year:
formed of 4 digits
Is my syntax correct?
list($name_month, $name_year) = explode('-', $name_date, 2);
if(($name_month < 1 || $name_month > 12 || $name_month ) || ($name_year)) {
echo "<br><br>Wrong date";
$uploadOk = 0;}
You can use the DateTime object and createFromFormat() to validate your date:
$date = '04-2000';
// Create a DateTime object pointing to the 1st of your given month and year
$d = DateTime::createFromFormat('d-m-Y', '01-' . $date);
if($d && $d->format('m-Y') == $date){
echo 'Valid date';
}
Eval.in
This should work, if seprator is always -
$date = '04-2000';
if($date == date('m-Y', strtotime('01-'.$date)))
{
echo 'Valid date';
}
As commented, there are several was to do this. What first comes to mind is checkdate() and createFromFormat().
The catch is, you need to be mindful what is injected for the day part since your format does not include day.
Since createFromFormat() injects the day part from the current date, it is not a viable option. George's code will fail for a format of 02-2015 on days after 28.
As such, I would use checkdate():
$date = '04-2000';
list($month, $year) = explode('-', $date);
if (checkdate($month, 1, $year)) {
echo 'Valid date';
}
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() )