How to remove to expiry time in php using mysql database - php

How to remove the expiry time in php using mysql database....
I want to remove the expiry time which are derived from mysql db..In my code i want to compare the show time from database with current time,to get the showtime which is greater than the current time.(Eg), showtime="10:30 AM,2:30 PM,6:30 PM,9:30 PM" and curent time is 03:21 PM.
Getting output:
{"offer_details":[{"showtime":["10:30 AM","2:30 PM","6:30 PM","9:30 PM"]}],"success":1,"message":"Successfully found "}
Expected output:
{"offer_details":[{"showtime":["6:30 PM","9:30 PM"]}],"success":1,"message":"Successfully found "}
$result=mysql_query("select * from offer_details where movieid='$movieid' and date_value='$date_value' and offer_status!='Expired'")or die (mysql_error());
$offer_details=array();
$offer_details['showtime']=explode(',',$row['showtime']);
$av = sizeof($offer_details['showtime']);
echo $showtime[i];
Thanks,

You can compare time via hour like this:
foreach($offer_details['showtime'] as $key=>$showTime){
if(date("H", strtotime($showTime)) < date("H", strtotime("now"))){
unset($offer_details['showtime'][$key]);
}
}
array_values($offer_details['showtime']);

When you explode the showtime string you actually obtain strings which are formatted as times but remain as strings. That is why you cannot get the result when you compare with the current time as strings ("8:00 AM" will always be greater than "3:00 PM"). I suggest you store the showtimes as different fields in your database (with datetime types) so that you only pick those greater than now. I hope it helps.

Try this example and see if this helps.
<?php
$offer_details['showtime']= ["10:30 AM","2:30 PM","6:30 PM","9:30 PM"];
//$now = strtotime('now');
$now = strtotime('3:30 PM');
$showtimes = array();
foreach($offer_details['showtime'] as $showtime) {
if(strtotime($showtime) >= $now) {
$showtimes[] = $showtime;
}
}
print_r($showtimes); //outputs Array ( [0] => 6:30 PM [1] => 9:30 PM )

Related

PHP date before a specified time

I know this has been asked in different ways but I'm struggling to get this to work.
I have a timestamp output from an API call in the following format:
2019-12-02T14:30:00
I'm capturing that in my PHP as follows
$person['StartDateTime']
I'm then formatting the timestamp and storing it into the $thetime variable like below.
$thetime = date("g.i",strtotime(date( $person['StartDateTime'])));
The echoed output of the $thetime to the browser gives me 14.00
Now I want to only have the if statement work if the $thetime holds a time before 14:00, if I try the following it does not work, I just get all of the times before and after.
if(strtotime($thetime) < strtotime('14') ) {
//shows all times before and after, no good...
}
if I do the following this works...
if(strtotime($thetime) < strtotime('now') ) {
//shows all times before 'now' but not what I want...
}
Any help would be much appreciated.
Fixed by formatting the date correctly first...
$starttime = date("H.i",strtotime(date($thetime)));
if(strtotime($thetime) < strtotime('14') ) {
//Will now only display times before 14 (2pm)
}

Not able to compare time properly in php

Here is a query that i am using to compare time in codeigniter
$present_time = date("h:i a");
$this->db->where('status!=', 'closed ');
$this->db->where('one_hour_break!=', ' ');
$this->db->where("(one_hour_break ='$present_time' or one_hour_break < '$present_time')");
$query = $this->db->get('student');
$final_time = $query->result();
if($final_time)
{
echo "true";
}
else
{
echo "false";
}
It is comparing the time properly if it is between 1 and 12 however it is not comparing time properly between the given time is between 12pm to 1pm and 12am to 1pm
e.g :
So if present time: 12:30 pm and one_hour_break: 1:30 pm, then one hour break should be greater than present time, and the result should say false, however it is saying true. Same goes with the time between 12pm to 1pm
Can anyone please tell how to manage this issue
There are a few ways to fix this, the best way is to go back and store the times in the database with "Y-m-d H:i" or something like it.
That way you can get a fixed timestamp of when, and not only time of day which is useless most of the times when you develop.
Then you use strtotime() http://php.net/manual/en/function.strtotime.php to convert the date in string format to a numeric value that can easily be compared.

Get the date from a time string

Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.
Examples:
If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.
If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.
I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.
I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.
Quick hack. Works as of today, 15.07.2013 23:58 local time:
$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
$nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
here is working example for you just add your dynamic variable to check date with user inputs
You can use mktime function to manage your date.
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));
echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;
if(strtotime($current_time) > strtotime($input_date)){
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));
echo "in";
}else{
// nothing to do
}
echo "<br> result->".$input_date;
i hope it will sure solve your issue

PHP - checking if two dates match but ignoring the year

I have an array which will output a date. This date is outputted in the mm/dd/yyyy format. I have no control over how this outputted so I cant change this.
Array
(
[date] => 04/06/1989
)
I want to use php to check if this date matches the current date (today), but ignoring the year. So in the above example I just want to check if today is the 6th April. I am just struggling to find anything which documents how to ignore the years.
if( substr( $date, 0, 5 ) == date( 'm/d' ) ) { ...
Works only if it's certain that the month and date are both two characters long.
Came in a little late, but here’s one that doesn’t care what format the other date is in (e.g. “Sep 26, 1989”). It could come in handy should the format change.
if (date('m/d') === date('m/d', strtotime($date))) {
echo 'same as today';
} else {
echo 'not same as today';
}
this will retrieve the date in the same format:
$today = date('m/d');
Use this:
$my_date = YOUR_ARRAY[date];
$my_date_string = explode('/', $my_date);
$curr_date = date('m,d,o');
$curr_date_string = explode(',', $date);
if (($my_date_string[0] == $curr_date_string[0]) && ($my_date_string[1] == $curr_date_string[1]))
{
DO IT
}
This way, you convert the dates into strings (day, month, year) which are saved in an array. Then you can easily compare the first two elements of each array which contains the day and month.
You can use for compare duple conversion if you have a date.
$currentDate = strtotime(date('m/d',time())); --> returns current date without care for year.
//$someDateTime - variable pointing to some date some years ago, like birthday.
$someDateTimeUNIX = strtotime($someDateTime) --> converts to unix time format.
now we convert this timeunix to a date with only showing the day and month:
$dateConversionWithoutYear = date('m/d',$someDateTimeUNIX );
$dateWithoutRegardForYear = strtotime($dateConversionWithoutYear); -->voila!, we can now compare with current year values.
for example: $dateWithoutRegardForYear == $currentDate , direct comparison
You can convert the other date into its timestamp equivalent, and then use date() formatting to compare. Might be a better way to do this, but this will work as long as the original date is formatted sanely.
$today = date('m/Y', time());
$other_date = date('m/Y', strtotime('04/06/1989'));
if($today == $other_date) {
//date matched
}
hi you can just compare the dates like this
if(date('m/d',strtotime($array['date']])) == date('m/d',strtotime(date('Y-m-d H:i:s',time()))) )

Looping through dates until a free one is found

I have a function which checks my database to see if a date exists, if it does exist, i want to display the next date which isnt in the database.
Is this possible?
My function returns 1 if there is a date in the database and 0 if there isnt, im using codeigniter, but not using any built in functions.
Its basically an availability checker, it allows us to input many different dates in the database, so calling my function i use
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y'));
The i use a if statement to check if the first time it runs it returns a value, this is how i have it
if($availcheck > 0){
// loop through the next dates and run the function again to see if it returns 0
} else {
echo 'available now';
}
I guess i would add 1 to the current date, check that one, then add another 1 and check that and so on.
Im just not sure how.
Cheers,
if i understand you correct , your problem is adding the day ?
if so i would suggest using the epoch or unix time
so convert the date to unix time using mktime than just add 1 day in seconds (24*60*60)
and then convert back to d/m/y format.
you can use the date function.
$date = time(); // get current timestamp
while ($availcheck) // while date IS found in database
{
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y',$date));
$date = $date + (24*60*60); // add one day
}
$date = $date - (24*60*60); // reduce one day
echo date('d/m/Y',$date); // prints the first date that is not in the DB
This SQL code could work for me.
$today = date("Y-m-d"); //today
$sql = "SELECT date FROM calendar WHERE date>'{$today}' AND date<='2100-12-31' AND date='0000-00-00' LIMIT 1";
Since you can't determine the ending date, 2100 could be for testing.

Categories