I want to be able to show some content or echo some text between and on two dates.
Its for a promotion of a product, between the dates given the price will be lower and after the promotion the price will return to the original cost.
Is this possible?
Cheers
Matt
$current_time = time();
if($current_time > $start_time && $current_time < $end_time){
// Whatever you want during the time interval
}else{
// Whatever you want outside the time interval
}
Related
I'm trying to write a function to calculate the next date that a piece of equipment needs to be checked. I'm using the code below (it's incomplete.)
function get_next_check(){
$today = date(get_option('date_format'));
$first_check = types_get_field_meta_value( 'first_check', $post_id );
// Interval is a number of weeks- ie. month = 4, year = 52
$interval = types_get_field_meta_value( 'interval', $post_id );
// Calculate the next date after today that the check needs to be performed
$next_check = ;
return $next_check;
}
add_shortcode( 'next_check', 'get_next_check' );
I'm guessing I need to create an array of all possible dates, and compare each to today's date, only returning the next one?
Assuming that $first_check is a string with the date in it, and you want the function to return the same, something like this might work:
$week = 60 * 60 * 24 * 7; // number of seconds in a week
$next_check = strtotime($first_check);
while ($next_check < time()) {
$next_check += $week * $interval; // advance the check time by the desired interval
}
return date(get_option('date_format'), $next_check);
You can eliminate your initialization of the $today variable with this.
You can achieve this on the proposed by Greg Schmidt , or alternatively by doing:
// Calculate the next date after today that the check needs to be performed
$next_check = date(get_option('date_format'), strtotime($today." ".$interval." weeks"));
I am using PHP, jQuery AJAX and HTML to create a timesheet system, for this the user needs to select 2 dates within 1 month of each other. The system as yet is working and shows (very limited) data.
BUT! When I actually select a date over the month limit (i.e. 2 months further than the start or another year after the start), it still shows the table with the data.
For this I have this check:
$dt1 = new DateTime($_REQUEST['startdate']);
$dt2 = new DateTime($_REQUEST['enddate']);
$diff = date_diff($dt1, $dt2);
// I have tried this the other way around and get the same result...
if($diff->m > 1 || $diff->y > 1)
{
print("<center><strong>Time between dates it too great<br />Please choose another date or time within a month of each other</strong></center>");
die();
}
The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:
11/14/2015 (start date) && 12/14/2015 (end date) - should show data
09/14/2015 (start date) && 12/14/2015 (end date) - should not show data but does
11/14/2015 (start date) && 12/14/2016 (end date) - should not show data but does
There is a check in place that sees if the dates given start before the other and this works, I have tried the same kind of thing for this check, but without success, this check is as such:
function CountDaysBetween($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "start date is in the future! <br />";
return;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days + 1;
}
}
Edit
Dates 2 or more months apart within the same year work, tested again and this is the case, but dates into the next year do not
In your first part of the php code, you have put this operator>, but the problem is it means, everything Smaller than 1, not everything that is smaller than one or equal to 1. The easy solution is to change the operators to >=; which means everything that is equal to 1 or smaller than 1.
The date_diff constructs in PHP suck monkeyballs. Far more practical is to use straight comparisons instead:
$dt1 = new \DateTime($_REQUEST['startdate']);
$dt2 = new \DateTime($_REQUEST['enddate']);
$dt1->add(new \DateInterval('P1M'));
echo ($dt1 < $dt2 ? 'Less' : 'More') . ' than a month';
Also please do not use $_REQUEST, it has potentially terrible security issues. You should use $_GET, $_POST or $_COOKIE according to what you explicitly expect.
I am trying to show different content based on the time recorded in my database.
From 1800 pm to 0800 am show content B.
The rest of the remaining time show content A.
My database field is storing time field so in database 1800 pm is stored as 18:00:00 same goes to 0800 am, it is stored as 08:00:00.
Below is my logic to get content B when time is between 18:00:00 to 08:00:00:
$now = strtotime(date('H:i:s'));
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
if($now >= $time_from && $now <= $time_to){
echo 'content A';
}
The above code will only work if my $time_to is within 23:59:59 as $now will always be bigger than 08:00:00. Lets say the time now is 23:30:00, my codes will never echo "content A" out because 23:30:00 is bigger than 08:00:00.
How can i make the logic work to check by time only then to display the content?
#All, im editing the code again. Yes. i did put $now = strtotime(date('H:i:s'));. But it is not working as well. Firstly, the current now unix timestamp will always be bigger than 08:00:00 unix timestamp. let's say the time now is 23:30:00. The unix timestamp will always be bigger than 08:00:00.
if (date("H:i") >= '08:00' && date("H:i") <= '18:00') {
// Retrieve content 'A'
} else {
// Retrieve content 'B'
}
before making the comparision, you need to strtotime() your $now variable as well, try:
$now = date('H:i:s');
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
$now_str = strtotime($now);
if($now_str >= $time_from && $now_str <= $time_to){
echo 'content A';
}
I need to get the days left before the date is reached, and if the timestamp is before the current date like 1990 or something, then it will display a message.
so say it's the 1st november of 2014
if the timestamp is before the first of november, 1st 2014, then it will display expired, else it will tell you how many days left before the date is reached.
thanks you very much.
This is in php by the way.
<?php
$current = time();
$target = '2014-11-01 00:00:00';
$target = strtotime($target);
if($target > $current){
$span = $target - $current;
$span = ceil($span / (60 * 60 * 24));
echo 'You have less than '.$span.' days!';
} else {
echo 'Time has already expired!';
}
The above will output
You have less than 39 days!
What i would do is set a DateTime Class for each unix timestamp you have. Compare the two Objects with DateTime's Diff Function.
$TodaysDate = new DateTime();
$TodaysDate->setTimestamp(time());
$ExperationDate= new DateTime('2014-10-01');
$interval = $TodaysDate->diff($ExperationDate);
If($interval <= 0){
echo 'Product Expired';
}
Their are multiple ways you can set the time in the DateTime Object, using timestamps or keywords or specific date formats.
Just subtract the current time from your timestamp and divide - Unix time is in seconds.
I have a field that has time and date in following format
2010-03-26 10:06:11
What I need is that if this time is within 4 hours of current time then show but if its over 4 hour then done show this record.
thanks
$ts = strtotime($value);
$curtime = time();
if (($ts > $curtime - 4*3600) && ($ts < $curtime + 4*3600)) {
//show
}
else
//don't show
You can also make a one-side comparison by choosing only one of the conditions (it isn't clear what you want from the question).