hello is there way to locked a page based on the input date. Like for example admins input june 18 , 2018 starttime to june 22, 2018 endtime . some users function is blocked or disabled until the starttime and be disabled again on the endtime. unfortunately i can't post a code because i dont know how to do it. any help is very much appreciated
Hello once solution is you need to change it in php.ini and disabled_function but that make not sense for user defined function and also can't change at run-time so i would like to prefer you alternate solution is
<?php
$startTime = strtotime('2008-09-22');
$endTime = strtotime('2008-09-25');
$now = new DateTime();
$currentDate = $now->format('Y-m-d');
$currentTime = $now->getTimestamp();
function a(){
if($currentTime>$startTime && $currentTime<$endTime){
// in this scope this is enable so you can put logic here
} else {
// in this scope this is disable section so you can return error warning and false
return false;
}
}
?>
Related
I have made this method for sending a temporary code to user's mobile phone number for verifying his account:
public function signInWithToken()
{
if(Session::has('nextStep') && Session::has('foundUser')) {
$user = User::where('usr_mobile_phone', Session::get('foundUser'))->first();
if(Session::has('codeSent')){
$nowTime = now();
$sentTime = Session::get('codeSent');
if($nowTime >= $sentTime){
$user->activeCode()->delete();
Session::forget('codeSent');
}
}else{
$code = ActiveCode::generateCode($user);
$user->notify(new ActiveCodeNotification($code, Session::get('foundUser')));
Session::put('codeSent', now()->addMinutes(10));
}
return view('frontend.auth.token');
}else{
abort(404);
}
}
So for the first time, a code generates and the session codeSent will also be submitted and its value is 10 minutes above the current time.
Then I also need to check if this session was already submitted, then it has to subtract the $nowTime from $sentTime to find out if the code was generated more than 10 minutes ago and is too old: (so the code must be deleted from the DB and the session must be forgotten)
$nowTime = now();
$sentTime = Session::get('codeSent');
if($nowTime >= $sentTime){
$user->activeCode()->delete();
Session::forget('codeSent');
}
But this code won't run and return this error:
Unsupported operand types: Illuminate\Support\Carbon - Illuminate\Support\Carbon
Value of nowTime var:
Value of sentTime var (set the currentTime + 10 minutes as the expire time of session codeSent):
And I know this is because those sentTime && nowTime variable values can not be subtracted from eachother somehow.
But really don't know what is the correct way for doing this check!
So if you know, please let me know, I would really appreciate any idea or suggestion from you guys...
you can use Carbon diffInSeconds like the code below(untested)
first you need to parse times with carbon
then use this:
$totalDuration = $finishTime->diffInSeconds($startTime);
// 21
If you need to find the difference in minutes
You can use it:
$nowTime = now();
$sentTime = Session::get('codeSent');
if($nowTime->diffInMinutes($sentTime)>10)
{ $user->activeCode()->delete(); Session::forget('codeSent'); }
I have written a function in PHP which will print the third Monday of every month.
I now want to test the function if the function will work next month, without having to wait till next month.
My idea is that I could test how the function would work in the future by internally setting PHP's time to next month and running the function.
I was hoping there would be a Date Time ini setting which I could insert before my function to shift the time into the future, something like
<?php
$one_month_from_now = time() + 2592000;
ini_set('php_internal_time', $one_month_from_now);
function print_third_monday() {
// ....
}
print_third_monday();
Is is possible to change PHP's internal time like this? if so, how?
echo date("y-m-d H:i:s D");//17-03-16 10:43:38 Thu
echo PHP_EOL;
echo date("y-m-d H:i:s D",strtotime("+1 month"));//17-04-16 10:43:38 Sun
How about a better function that does the same functionality & you can cross check with ease:
<?php
echo getThirdTuesdayofMonth(3,2017);
function getThirdTuesdayofMonth($month, $year) {
$thirdTuesday='';
$no_of_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$tue_iter=0;
for($i=1;$i<=$no_of_days;$i++) {
if(date('l',strtotime($i.'-'.$month.'-'.$year))=='Tuesday') {
if($tue_iter==3) {
$thirdTuesday = date('dS F Y', strtotime($i.'-'.$month.'-'.$year));
}
$tue_iter++;
}
}
return $thirdTuesday;
}
I'm using Live Connect to create calendar events. According to their docs, the start_time given for an event should indicate how many hours off the time is from UTC (i.e. +0700 or -0300). As a first stab , I've got some code that works, pieced together from the php manual. However, it "feels" pretty verbose. So, from a stylistic point of view, might there be a way to clean up what I've got into something more succinct? Note that the $time_zone is something that I know based on a given user.
$dateTimeZone = new DateTimeZone($time_zone);
$dateTime= new DateTime("now", $dateTimeZone);
$gmt_offset = ($dateTime->getOffset())/3600;
$negative = ($gmt_offset<0);
$gmt_offset = abs($gmt_offset);
if ($gmt_offset < 10) {
$gmt_offset = '0'.$gmt_offset.'00';
} else {
$gmt_offset = $gmt_offset.'00';
}
if ($negative) {
$gmt_offset = '-'.$gmt_offset;
} else {
$gmt_offset = '+'.$gmt_offset;
}
Thank you for your input.
-Eric
$gmt_offset = $dateTime->format('O');
From the PHP manual page for date():
format character: O
Description: Difference to Greenwich time (GMT) in hours
Example returned values: Example: +0200
My server has many application instances.
I came across a problem that one of my application instance needs to be tested with the future date. i.e I want to test the application as it is running in 2013.
If i change the system date then it will work fine but the other instances will also get effected.
I want the future date for only one instance and the rest should work as it is.
i.e if i use date('Y-m-d'); it should jump for 3 months and display the future date.
and i dont want to add seconds to the default date as that might be a huge change in my application.
And that's why you write your application in a way that is testable.
Not good:
function doSomething() {
$date = date('Y-m-d');
...
}
Good:
function doSomething($ts = null) {
if (!$ts) {
$ts = time();
}
$date = date('Y-m-d', $ts);
...
}
you can make your own date function. It would serve as a hook to all date usage.
function mydate($format) {
$jump = ' +3 months';
return date($format, strtotime(date($format) . $jump));
}
you can than change all occurrences of date to mydate. If you decide to switch back to present, just leave $jump = ''
You can just do
date('Y-m-d', time() + 3 * 30 * 24 * 3600);
I recommend using the PHP5 DateTime classes. They're a bit more wordy, but much more powerful than the old-style PHP date handling functions.
$dateNow = new DateTime();
$dateAhead = $dateNow->add(DateInterval::createFromDateString('3 months'));
print $dateAhead->format('Y-m-d');
I want to have code in a loop that only runs if it is before 5pm on any day.
What code would do this?
IF (current_time < 5:00pm) {
// Do stuff
}
This solution uses the nice PHP5 DateTime class, and avoids the clunky old strtotime() function:
$hoursNow = new DateTime()->format('H');
if($hoursNow < 17) {
....
}
Simple one that gets the job done:
if (date('H') < 17)
{
// DO SOMETHING
}
Just remember that this date comes from the server. So, if the client is in another timezone, that may not work as required. The good thing about this, is that the user can't change his computer date in order to trick the site into something.
And if you want to do that with javascript (where the value will come from the user's computer), just do the following:
var today = new Date();
if (today.getHours() < 17)
{
// DO SOMETHING
}
if(time() < strtotime('today 05:00 pm')) {
// Do stuff
}
In short...
$t = time(); # or any other timestamp
if (date('H', $t) < 17) {
// Do stuff
}
Works for any day
Note: Make sure your timezone is correct (check php config or just use date_default_timezone_set function before calling date function)