PHP Trigger date? xmas website preparation - php

I am prepping our businesses website for the Christmas period between November 1st until December 31st at midnight. I have already made the amendments on the sites CSS style-sheet to trigger the Christmas theme overwrite when the class of "xmas" is added to the body element if have placed the code I have for this into our global head.php file but it's triggering just now and I am trying to catch this within PHP tags to trigger with the date function?
Is this possible - If so could someone help?
So far I have tried this...
<?php
//Must be in format of DAY-MONTH-YEAR
if(date('d-j-Y') == "30-11-date('Y')") {
?>
<script>
$(document).ready(function(){
$('body').addClass('xmas');
});
</script>
<?php
}
?>
But of course this will only work on the 30th of November and I need this to work though until the 31st of December.

Use DateTime objects are they are comparable and make this fairly easy and straightforward.
<?php
$now = new DateTime();
$nov1 = new DateTime('2015-11-01 00:00:00');
$dec31 = new DateTime('2015-12-31 23:59:59');
if($now >= $nov1 && $now <= $dec31) {
?>
If you want to make the code dynamic each year just make the year value dynamic as you have in your example.

To have a script that whatever is the year or date, it always tell you if its time of xmas and new year eve holidays
i have use it in PHP
/*
* ==12 if is decembre, >= 17 if day is great than 17
* ==12 if is january, <= 7 if day is less than 7
* #return if is holiday time or not
*/
public static function isHolidayTime()
{
if(date("m") == 12 && date("d") >= 17){
//Holiday time there
return true;
}else if(date("m") == 1 && date("d") <= 7){
//Holiday time there
return true;
}
else{
//normal day
return false;
}
}

You can use the date function with month and day, like so:
$date = date('md');
if($date >= '1130' && $date <= '1231')

Related

hide message only during the set time

I am trying to hide a message between 2 particular timings and rest of the timings it should display.
the timings are
07:30am to 10:30am
and
18:30pm to 00:00am midnight
currently i did the following code which works ok for 07:30am to 10:30am it does the job but now how do i combine the 18:30pm to 00:00am midnight??? please someone help me out with some good logic.
<?php
date_default_timezone_set('America/New_York');
$now = new DateTime();
$fromTime = new DateTime();
$fromTime->setTime(07,30);
$toTime = new DateTime();
$toTime->setTime(10,30);
$fromTime1 = new DateTime();
$fromTime1->setTime(18,30);
$toTime1 = new DateTime();
$toTime1->setTime(00,00);
if ( $now < $fromTime || $now > $toTime) {
?>
This is a test message
<?php
}
?>
I tried doing like this
$now < $fromTime || $now > $toTime && $now < $fromTime1 || $now > $toTime1
but it messes up and displays the message even during 07:30am.
Thanks for your help
It's prety simple, to check few dates interval, but, don't make mistakes with PHP logical operators (https://www.php.net/manual/en/language.operators.logical.php) :
if ( ($now >= $fromTime && $now <= $toTime) // If it's on the first interval
|| // Or
($now >= $fromTime1 && $now <= $toTime1) // If it's on the second one
) {
echo 'Appear on the interval'; // I display the message.
}
Also, you've a mistake with your last variable $toTime1, you must to check from 18::00 to 00:00 ... Not from the current day ! (but 00:00 to the next day).
You can edit your code like this, by addind +1 day to your last variable :
$toTime1 = new DateTime();
$toTime1->modify('+1 day');
$toTime1->setTime(00,00);
Like this, your timings are good ;)
Don't forget this code (php) is managed by the server side. So, if your user don't reload the page, your message will be visible during your intervals. Maybe you can use JavaScript for hide the message during your intervals on the client side, in the case that the user remains on the page ;)
Hope I help you.

Date & time comparison operators >= not showing dates that are equal

I am trying to show events that occur either today or on a later date where today is specifically the problem.
public function getInspirationsMeetingIds()
{
$ids = [];
if (($inspirationMeetings = $this->getCustomField('meetings'))) {
foreach ($inspirationMeetings as $meeting) {
$row = new Inspiration($meeting['meeting']);
$dateFrom = $row->getCustomField('date');
if (strtotime($dateFrom) >= time()) {
$ids[] = $row->getId();
}
}
}
return $ids;
}
For some reason this will only show events that are greater than time() and not the events that are today, but then when i try this:
if (strtotime($dateFrom) <= time()) {
$ids[] = $row->getId();
}
Today's and older events are shown.
I think you need to add a timestamp to your datefrom.
Strtotime will add noon if time is omitted.
See this example https://3v4l.org/cYKO4
if (strtotime($dateFrom ) >= strtotime(date("Y-m-d 00:00"))) {
Will make it show all of datefrom
Edit added the 00:00 at the wrong side
Use the DateTime class http://php.net/manual/en/class.datetime.php
time() gives seconds since Jan 1st 1970. The chance that you hit the exact second is very small, so it will hardly ever match.
Instead, create a date with the time.
$date = new DateTime($dateFrom); // or DateTime::createFromFormat($format, $dateFrom);
$today = new DateTime();
if ($date >= $today) {
// should work
}

Finding the difference between 2 dates in PHP

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.

how can i get all next dates with day from given date which is based on weekly, biweekly, monthly in php?

I want all next dates with day from specified date for sending mail within cron job file.
right now I m checking for whether its week by calculating like this
$event_from_date = strtotime(date('Y-m-d',strtotime($rs->from_date)));
$today_date = strtotime(date('Y-m-d'));
$event_expire_on = strtotime($rs->to_date);
if($rs->time_zone == "CST")
$current_date=strtotime($cst_date);
elseif($rs->time_zone == "PST")
$current_date=strtotime($pst_date);
elseif($rs->time_zone == "MST")
$current_date=strtotime($est_date);
elseif($rs->time_zone == "EST")
$current_date=strtotime($mst_date);
if($current_date <= $event_expire_on && $current_date >= $event_from_date)
{
$diff=$today_date-$event_from_date;
if($diff%604800 == 0 && $rs->report_cycle=="Weekly")
$flag = 1;
else
$flag = 0;
}
can any body tell me . how can I get all next 7 days for weekly,15 days for biweekly,30 days for monthly with its day like Monday,Tuesday..
I'm not 100% sure I have understood your question. The DateTime classes offer simple ways of manipulating dates and times in all manner of ways.
I think, in your position I would be looking for DatePeriods to represent the various erm, periods I need. A function like this may suit:-
/**
* #param Int $days
* #return DatePeriod
*/
function getNumDays($days)
{
$today = new \DateTime('UTC');
$oneDay = new \DateInterval('P1D');
return new \DatePeriod($today, $oneDay, $days);
}
foreach(getNumDays(7) as $day){
//Do what ever you want with the DateTime instance
//We'll just var_dump() it for now.
var_dump($day);
}
See it working.

How do I get next occurrence of a certain day of the month

I am trying to get stripe to set a end_trial date on the next occurrence of whatever day of the month the user chooses. i.e. If today is the 16th and the user chooses the 15th I need the unix timestamp for the 15th of the next month. However if today was the 14th I need the timestamp for tomorrow.
I tried the solution found on this SO question Find the date for next 15th using php .
When i ran the code suggested in that question and substituted 15 for 31
$nextnth = mktime(0, 0, 0, date('n') + (date('j') >= 31), 31);
echo date('Y-m-d', $nextnth);
The result is 2013-03-03
I also tried this one Get the date of the next occurrence of the 18th .
The second one would actually give me 2013-03-31 when i ran it one 2013-1-31.
Both had unexpected results. Is february the problem? Any guidance will be much appreciated.
Here is a way to do it.
function nextDate($userDay){
$today = date('d'); // today
$target = date('Y-m-'.$userDay); // target day
if($today <= $userDay){
$return = strtotime($target);
}
else{
$thisMonth = date('m') + 1;
$thisYear = date('Y');
if($userDay >= 28 && $thisMonth == 2){
$userDay = 28;
}
while(!checkdate($thisMonth,$userDay,$thisYear)){
$thisMonth++;
if($thisMonth == 13){
$thisMonth = 1;
$thisYear++;
}
}
$return = strtotime($thisYear.'-'.$thisMonth.'-'.$userDay);
}
return $return;
}
// usage
echo date('Y-m-d',nextDate(29));
We get the user's choice and compare it today.
If today is less than or equal to user choice, we return the timestamp for this month.
If today is greater than user choice, we loop through dates, adding a month (or a year if it's $thisMonth hits 13). Once this date does exist again, we have our answer.
We check the dates using php's checkdate function, strtotime and date.
I really don't understand the question completely. You can easily determine the date for next 30 days for example
$next_ts = time() + 30 * 86400; // add 30 days to current timestamp
$next = date('Y-m-d', $next_ts); // format string as Y-m-d
echo $next;
If that is not what you need, please explain the problem.

Categories