How to change timezone of date and strtotime function in laravel - php

I have changed timezone with 'Asia/Kolkata' in app.php
Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'Asia/Kolkata',`
Now I trying to calculation between two time than output giving +5:30 time
how to fix this issue?
example:-
$inTime = "10:08:00";
$outTime = "10:08:00";
$spentTime = strtotime($outTime) - strtotime($inTime);
echo "<pre>";
print_r("out time = ".$outTime);
echo "<br>";
print_r("in time = ".$inTime);
echo "<br>";
print_r("spent time = ".$spentTime);
$spentTime = **date('H:i:s', $spentTime)**;
echo "<br>";
print_r("spent time format = ".$spentTime);
die;
Output:-
out time = 10:08:00
in time = 10:08:00
spent time = 0
spent time format = 05:30:00
I expecting "spent time format" given = 00:00:00
What can we do so that we can achieve this?
Thank You!

try to use Carbon in your prject.
$inTime = Carbon::parse('10:08:00', 'Asia/Kolkata');
$outTime = Carbon::parse('10:08:00', 'Asia/Taipei');
//You can replace diffInSeconds with other diff methods.
$spentTime = $outTime->diff($inTime)->format('%H:%I:%S');

Related

PHP UTC String to date

I have a problem. In my code I have the following line:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
The goal is to get the previous and next hour, so I tried this:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$epochRunDateTimeGMT0 = strtotime($RunDateTimeGMT0);
$previousEpochDateTimeGMT0 = $epochRunDateTimeGMT0 - 3600;
$nextEpochDateTimeGMT0 = $epochRunDateTimeGMT0 + 3600;
But then the I get the following result:
previousEpochDateTimeGMT0 -> 2017-12-31 21:00:00
nextEpochDateTimeGMT0 -> 2017-12-31 23:00:00
Because of my timezone (+1) the RunDateTimeGMT0 gets converted to a date of my timezone.
I want the following result:
validEpochDateTimeGMT0 -> 2017-12-31 22:00:00
nextEpochDateTimeGMT0 -> 2018-01-01 00:00:00
How can I keep the date object UTC?
You can use the Carbon library. after import this library you should parse the date with this :
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$epochRunDateTimeGMT0 = Carbon::parse($RunDateTimeGMT0);
in this link, you could see the documentation of Carbon. Although, maybe you should to use this method :
$previousEpochDateTimeGMT0 = $epochRunDateTimeGMT0->addminutes(60);
$nextEpochDateTimeGMT0 = $epochRunDateTimeGMT0->subminutes(60);
I hope your problem solve with these lines, If another issue occurred you can ask.
#Ebrahim Bashirpour already shared a way of doing this by using the Carbon library but you can also do this by just using the PHP Datetime class. It supports both add time and subtracts time in seconds. Take a look at the DateTime documentation for more details.
<?php
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$date = new \DateTime($RunDateTimeGMT0);
$date->add(new \DateInterval('PT3600S')); //add 3600s / 1 hour
$next_epoc = $date->format('Y-m-d H:i:s'); // 2018-01-01 00:00:00
$date = new \DateTime($RunDateTimeGMT0);
$date->sub(new \DateInterval('PT3600S'));//add 3600s / 1 hour
$previous_epoc = $date->format('Y-m-d H:i:s'); //2017-12-31 22:00:00
var_dump($next_epoc);
var_dump($previous_epoc);
?>

PHP - Check difference between 2 different time format

I'm saving an updated_at time in the database which is being handled by laravel.
I want to check the difference between a field updated_at value and Now time in minutes .
This is how i get current time :
$now = time();
This is my echo results :
UPDATED AT : 2017-09-10 13:14:29
TIME NOW : 1505051247
Any idea how to check the time between these 2 values by minutes.
Thanks
You are using Laravel so use power of Carbon
$updated_at = \Carbon\Carbon::parse('2017-09-10 13:14:29');
$timeNow = \Carbon\Carbon::createFromTimestamp(1505051247); // but you should use \Carbon\Carbon::now()
$diffInMinutes = $updated_at->diffInMinutes($timeNow);
dd($diffInMinutes);
Use Carbon
use Carbon;
$time_now = Carbon::now();
$updated_at = Carbon::parse('2017-09-10 13:14:29');
$differenceInMinutes = $updated_at->diffInMinutes($time_now);
Hope it's helpful.

In an LDAP 'lastlogon' lookup how do I decipher the result?

I'm using a PHP script to grab data from Active Directory using LDAP..
When I get the user values for 'lastlogon' I get a number like 129937382382715990
I've tried to figure out how to get the date/time from this but have no idea, can anybody help?
Read this comment on the PHP: LDAP Functions page.
All of them are using "Interval" date/time format with a value that represents the number of 100-nanosecond intervals since January 1, 1601 (UTC, and a value of 0 or 0x7FFFFFFFFFFFFFFF, 9223372036854775807, indicates that the account never expires): https://msdn.microsoft.com/en-us/library/ms675098(v=vs.85).aspx
So if you need to translate it from/to UNIX timestamp you can easily calculate the difference with:
<?php
$datetime1 = new DateTime('1601-01-01');
$datetime2 = new DateTime('1970-01-01');
$interval = $datetime1->diff($datetime2);
echo ($interval->days * 24 * 60 * 60) . " seconds\n";
?>
The difference between both dates is 11644473600 seconds. Don't rely on floating point calculations nor other numbers that probably were calculated badly (including time zone or something similar).
Now you can convert from LDAP field:
<?php
$lastlogon = $info[$i]['lastlogon'][0];
// divide by 10.000.000 to get seconds from 100-nanosecond intervals
$winInterval = round($lastlogon / 10000000);
// substract seconds from 1601-01-01 -> 1970-01-01
$unixTimestamp = ($winInterval - 11644473600);
// show date/time in local time zone
echo date("Y-m-d H:i:s", $unixTimestamp) ."\n";
?>
This is the number 100-nanosecond ticks since 1 January 1601 00:00:00 UT.
System time article in Wikipedia can give you more details.
What about this:
$timeStamp = 129937382382715990;
echo date('Y-m-d H:i:s', $timeStamp);
EDIT ------
I just tried the following and noticed that this method wont work unless the clock on your machine is set 10 years in the future. Below is the code I used to prove the above pretty much useless unless you do more processing maybe..
$time = time();
echo date('Y-m-d H:i:s', $time);
echo "<br />";
$timeStamp = 129937382382715990;
echo date('Y-m-d H:i:s', $timeStamp);
In my case I'm using Pentaho. With a Modified Javascript value you can convert the values, lastLogon is the column I wanna convert from data stream:
calendar = java.util.Calendar.getInstance();
calendar.setTime(new Date("1/1/1601"));
base_1601_time = calendar.getTimeInMillis();
calendar.setTime(new Date("1/1/1970"));
base_1970_time = calendar.getTimeInMillis();
ms_offset = base_1970_time - base_1601_time;
calendar.setTimeInMillis( lastLogon / 10000 - ms_offset); //lastLogon is a column from stream
var converted_AD_time = calendar.getTime(); // now just add this variable 'converted_AD_time' to the 'Fields' as a show in the image below

Get time from a date/time string

I have a date value stored in a variable. I need to extract the time part of the value in to a separate variable and then add/subtract time from it.
The date variable is set with date('YmdHis'), giving (for example) 20110805124000 for August 5th 2011, 12:40:00
From the value 20110805124000 (which is stored in the variable $fulltime), I need to store the time only in the format 12:40 (so ignoring the year, month, day and seconds and adding the colon between the hour and minute) in a variable called $shorttime. I then need to add a number of hours to that time (so for example +3 hours would change the value in the $shorttime variable to 15:40). The number of hours I need to add is stored in a variable called $addtime, and this value could be a negative number.
Is this easily doable? Could anyone help?
Thanks :)
$time = '2013-01-22 10:45:45';
echo $time = date("H:i:s",strtotime($time));
It will give the time 10:45:45 from datetime.
<?PHP
$addhours = 3;
$date = DateTime::createFromFormat('YmdHis', '20110805124000');
$shorttime = $date->format("H:i");
$newdate = $date->add(DateInterval::createFromDateString($addhours . "hours"));
$newtime = $newdate->format("H:i");
echo $shorttime . "<br />";
echo $newtime . "<br />";
?>
for your reference:
http://www.php.net/manual/en/datetime.createfromformat.php
http://www.php.net/manual/en/dateinterval.createfromdatestring.php
hello the way I usually do it is like this, maybe it's a bit long but it works for me...
$DateIn = new DateTime($In);
$DateOut = new DateTime($Out);
$HourOut = new DateTime($H_Out);
$FechaEntrada = $DateIn->format('d-m-Y');
$FechaSalida = $DateOut->format('d-m-Y');
$HoraSalida = $HourOut->format('H:i a');
the guide I used was the PHP one DateTime::format()

Behavior of <?php echo date("H:i:s", 0);?>

I used :
<?php
echo date("H:i:s", $var_time_diff);
?>
to construct a time between two dates.. and in my head it was
$var_time_diff = 36000 = display 10:00:00 for 10 hours.
But in fact
<?php echo date("H:i:s", 0);?>
display 01:00:00 and not 00:00:00.
So we have
$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
All is ok for the moment $diff is 5 hours but if we display date like this:
echo date("H:i:s", $diff);
it will be "06:00:00".
So something wrong with my php config or it's a normal behavior for php function date?
The date() function uses your current time zone. If you want to ignore your configured time zone, use date_default_timezone_set() or use gmdate().
You're in some timezone other than UTC. Try:
<?php
date_default_timezone_set('UTC');
echo date("H:i:s",0) . "\n";
I'm not sure why, but date outputs the current hour in your example, make a timestamp from your seconds first and it works. I'll be following this question for a deeper explanation though.
$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
echo date("H:i:s", mktime(0,0,$diff));
edit: ah, so it adjusts to your current timezone, so does mktime, so the effect is negated.
use mktime, and min 1 for hour # $diff

Categories