Event date and time not select in datepiker less then server time - php

User careate feture event using bootstrep datepiker
User select date and time greter then currant time
4.If user select date Event time is 04-09-14 11:55AM and server time is 04-09-14 11:60AM
after not valid date for create event.
I am Using this code.
$event_time =htmlentities($_REQUEST['date_piker']) ;
$server_time = htmlentities( date('m-d-y h:iA')) ;
echo 'Event time:- '. $event_time.'</br>';
echo 'Server Time:-'. $server_time;
if($event_time < $server_time)
{
echo 'Date and time is invalid';
}
else
{
echo 'Date and time is valid';
}
But It is not working. i need help.

You should use DateTime::createFromFormat
$event = DateTime::createFromFormat('m-d-y h:iA', '04-09-14 11:55AM');
$server = DateTime::createFromFormat('m-d-y h:iA', '04-09-14 11:00AM');
if($event < $server){
echo 'Date and time is invalid';
}else{
echo 'Date and time is valid';
}

why u didnt use timestamps? like this
<?php
$event_time = htmlentities($_REQUEST['date_piker']);
$eventTimeTimestamp = strtotime($event_time);
$serverTimeTimestamp = time();
$server_time = htmlentities(date('m-d-y h:iA', $serverTimeTimestamp));
echo 'Event time:- ' . $event_time . '</br>';
echo 'Server Time:-' . $server_time;
if ($eventTimeTimestamp < $serverTimeTimestamp) {
echo 'Date and time is invalid';
} else {
echo 'Date and time is valid';
}

Related

If else statement for PHP DateTime diff

I am using meta_box value (YmdHi) and current Date&time to print time difference. And this code work for me.
Now additionally i want to print Live when 2 hours left to start Event.
What mistake I 'm doing to print if or else currently this not work for me?
$then = date( get_post_meta( get_the_ID(), '_start_eventtimestamp', true ) ) ;
$then = new DateTime($then);
$now = new DateTime();
$sinceThen = $then->diff($now);
if ($sinceThen > 2 * HOUR_IN_SECONDS){
echo $sinceThen->d.'days';
echo $sinceThen->h.'hours';
echo $sinceThen->i.'minutes';
}
else{
echo 'LIVE';
}
$sinceThen is a DateInterval (that's what DateTime::diff() returns), so you're comparing a int with an object, which obviously gives you unexpected results. To get the difference in seconds, subtract both DateTime instances' timestamps (which you obtain with DateTime::getTimestamp()):
$then = date( get_post_meta( get_the_ID(), '_start_eventtimestamp', true ) ) ;
$then = new DateTime($then);
$now = new DateTime();
$sinceThen = $then->diff($now);
$sinceThenInSeconds = $then->getTimestamp() - $now->getTimestamp();
if ($sinceThenInSeconds > 2 * HOUR_IN_SECONDS){
echo $sinceThen->d.'days';
echo $sinceThen->h.'hours';
echo $sinceThen->i.'minutes';
}
else{
echo 'LIVE';
}

Date format conversion issue PHP

See the below code returns the output as "Jun".
$date_removed =strtotime('2017-06-07 06:06:44');
$datev = date('d-m-Y', $date_removed);
$nameOfMonth = date('M', strtotime($datev));
echo $nameOfMonth;
But the below code returns "Dec" as output.
$date_removed =strtotime('0000-00-00 00:00:00');
$datev = date('d-m-Y', $date_removed);
$nameOfMonth = date('M', strtotime($datev));
echo $nameOfMonth;
Here the month value is null. So the return value should be null. Please let me know the issue in my code?
you could try to do it like this:
try {
$dt = new \DateTime('0000-00-00 00:00:00');
} catch (Exception $e) {
var_dump($e);
}
<?php
date_default_timezone_set("Canada/Central");
$date_removed = mktime(00, 00, 0000);
if($date_removed)
{
$datev = date('Y/m/d',$date_removed);
$nameOfMonth = date('M', strtotime($datev));
echo $nameOfMonth;
}
else
echo "invalid date"; //or whatever you want
?>
strtotime() will return false if the date is invalid so you can check that using the return type:
<?php
$date_removed =strtotime('0000-00-00 00:00:00');
if($date_removed>0)
{
$datev = date('d-m-Y', $date_removed);
$nameOfMonth = date('M', strtotime($datev));
echo $nameOfMonth;
}
else
echo "invalid date"; //or whatever you want

PHP Conditional dates treatment - comparing times & dates

I want to make a condition for my function to work. This is the logic.
Booking closing is 12noon today.
If the current time is before closing time(say 11am today) then run function x. I need to set a time when booking closes and compare it to the current time.
$tz_object = new DateTimeZone('Africa/Kampala');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
$timeNow = $datetime->format('Y\-m\-d\ h:i:s');
$date = new DateTime();
$date->setTime(23,00);
$timeAllowed = $datetime->format('Y\-m\-d\ h:i:s');
if ($timeNow < $timeAllowed) {
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), number_format(5) );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
}
Comparing DateTime objects is easy. You do not need to convert the date time to a string.
// time when booking closes
$bookclose = new \DateTime();
$bookclose->setTimezone(new DateTimeZone('Africa/Kampala'));
$bookclose->setTime(12,0,0);
// fake a time that the booking is being made for testing
$bookingtime = new \DateTime();
$bookingtime->setTimezone(new DateTimeZone('Africa/Kampala'));
$bookingtime->setTime(11,59,59);
echo 'Booking time is ' . $bookingtime->format('d/m/Y H:i:s');
if ($bookingtime < $bookclose) {
echo ' ALLOWED'.PHP_EOL;
} else {
echo ' NOT ALLOWED'.PHP_EOL;
}
$bookingtime->setTime(12,0,0);
echo 'Booking time is ' . $bookingtime->format('d/m/Y H:i:s');
if ($bookingtime < $bookclose) {
echo ' ALLOWED'.PHP_EOL;
} else {
echo ' NOT ALLOWED'.PHP_EOL;
}
$bookingtime->setTime(12,0,1);
echo 'Booking time is ' . $bookingtime->format('d/m/Y H:i:s');
if ($bookingtime < $bookclose) {
echo ' ALLOWED'.PHP_EOL;
} else {
echo ' NOT ALLOWED'.PHP_EOL;
}
RESULTS:
Booking time is 19/01/2017 11:59:59 ALLOWED
Booking time is 19/01/2017 12:00:00 NOT ALLOWED
Booking time is 19/01/2017 12:00:01 NOT ALLOWED

PHP display data

Guys we have a website that records the travel time of the vehicle. We have different kinds of reports. The problem is that our 2 reports is not the same on the last data. I think our problem is on the syntax on the display of our report.
here are sample screenshots of our reports. the KMLOG and the Daily Report. our problem is we need to display the output of the
]
if($endidling){
$total_idling=xtimediff($s_idling_date." ".$s_idling_time,$e_idling_date." ".$e_idling_time);
//if ($total_idling>=($entry['ilimit']*60)){
if($s_idling_date==$e_idling_date){
$data[$myCount]["xtype"] = "idling";
$data[$myCount]["dur"] = sec2hms($total_idling);
$data[$myCount]["sdate"] =date('M d',strtotime($s_idling_date));
$data[$myCount]["stime"] =$s_idling_time." to ".$e_idling_time;
$data[$myCount]["location"] = $location;
$myCount +=1;
}
else{
//$data[$myCount]["stime"] =$s_idling_time." to ".$e_idling_time." of ".date('M d',strtotime($e_idling_date)) ;
$day_def=def_date($s_idling_date,$e_idling_date);
for($day=0;$day<=$day_def;$day++){
if($day==0){
$ddur = xtimediff($s_idling_date." ".$s_idling_time,$s_idling_date." ".$gabi);
$data[$myCount]["stime"] =$s_idling_time." to ".$gabi;
$next_day=$s_idling_date;
}
elseif($day==$day_def){
$ddur = xtimediff($e_idling_date." ".$umaga,$e_idling_date." ".$e_idling_time);
$data[$myCount]["stime"] =$umaga." to ".$e_idling_time;
$next_day = date('Y-m-d', strtotime($next_day . ' +1 day'));
}
else{
$ddur = xtimediff($s_idling_date." ".$umaga,$s_idling_date." ".$gabi);
$data[$myCount]["stime"] =$umaga." to ".$gabi;
$next_day = date('Y-m-d', strtotime($next_day . ' +1 day'));
}
//if($day==0) $ddur= xtimediff($s_off_date." ".$s_off_time,$e_off_date." ".$e_off_time);
$data[$myCount]["xtype"] = "idling";
$data[$myCount]["dur"] = sec2hms($ddur);
$data[$myCount]["sdate"] =date('M d',strtotime($next_day));
$data[$myCount]["location"] = $location;
$myCount +=1;
}
}
//}
$endidling=false;
$total_idling=0;
}

PHP if based on current system date

Trying to setup a page that auto updates based on the users date/time.
Need to run a promotion for 2 weeks and each day it needs to change the displayed image.
Was reading through http://www.thetricky.net/php/Compare%20dates%20with%20PHP to get a better handle on php's time and date functions.Somewhat tricky to test, but I basically got stuck on:
<?php
$dateA = '2012-07-16';
$dateB = '2012-07-17';
if(date() = $dateA){
echo 'todays message';
}
else if(date() = $dateB){
echo 'tomorrows message';
}
?>
I know the above function is wrong as its setup, but I think it explains what I am aiming for.
Time is irrelevant, it needs to switch over at midnight so the date will change anyway.
You seem to need this:
<?php
$dateA = '2012-07-16';
$dateB = '2012-07-17';
if(date('Y-m-d') == $dateA){
echo 'todays message';
} else if(date('Y-m-d') == $dateB){
echo 'tomorrows message';
}
?>
you want
<?php
$today = date('Y-m-d')
if($today == $dateA) {
echo 'todays message';
} else if($today == $dateB) {
echo 'tomorrows message';
}
?>
I would go a step back and handle it via file names. Something like:
<img src=/path/to/your/images/img-YYYY-MM-DD.jpg alt="alternative text">
So your script would look something like this:
<img src=/path/to/your/images/img-<?php echo date('Y-m-d', time()); ?>.jpg alt="alternative text">
If you're going to do date calculations, I'd recommend using PHP's DateTime class:
$promotion_starts = "2012-07-16"; // When the promotion starts
// An array of images that you want to display, 0 = the first day, 1 = the second day
$images = array(
0 => 'img_1_start.png',
1 => 'the_second_image.jpg'
);
$tz = new DateTimeZone('America/New_York');
// The current date, without any time values
$now = new DateTime( "now", $tz);
$now->setTime( 0, 0, 0);
$start = new DateTime( $promotion_starts, $tz);
$interval = new DateInterval( 'P1D'); // 1 day interval
$period = new DatePeriod( $start, $interval, 14); // 2 weeks
foreach( $period as $i => $date) {
if( $date->diff( $now)->format("%d") == 0) {
echo "Today I should display a message for " . $date->format('Y-m-d') . " ($i)\n";
echo "I would have displayed: " . $images[$i] . "\n"; // echo <img> tag
break;
}
}
Given that the promotion starts on 07-16, this displays the following, since it is now the second day of the promotion:
Today I should display a message for 2012-07-17 (1)
I would have displayed: the_second_image.jpg

Categories