Change getInternalDate into user readable date? - php

I got messages by gmail api php,here I have tried to get date by getInternalDate is output long number!So I want to change user readable date but I can't when format by date() using php!!
$single_message = $service->users_messages->get('me', $message_id, $optParamsGet2);
$date = $single_message->getInternalDate();//'1464161738000'
var_dump(date("Y",$date));// 1956 ,should be 2016

Seems that getInternalDate return a value in miliseconds instead of second.
so you just have to divide it by 1000 and then use the date function.
$date = $single_message->getInternalDate() / 1000;
var_dump(date("Y-m-d H:i:s", $date));

Divide $date by 1000 :) the internalDate is in ms.

Related

how to convert Integer values into timestamp format?

I have an Integer column "duration_temp" that have values represent the duration in minutes, I want to copy those values in another column "duration" of type timestamp, I'm having the problem of how to convert those Int minutes into timestamps format, for example:
if a value in Int is set to 4 then I should convert it to yyyy-mm-dd 00:04:00.
is there a function that can do that or close from doing that?any suggestion would be appreciate it.
If you have a duration in minutes. You could use DateInterval like this.
$yourDate = new DateTime('2021-01-01 00:00:00');
$durationInMinutes = 4;
$interval = new DateInterval("PT{$durationInMinutes}M");
$yourDate->add($interval);
echo $yourDate->format('Y-m-d H:i:s');
https://www.php.net/manual/en/dateinterval.construct.php
If you mean that you just have minutes and want to make a timestamp from it with current date information, try this (after adding use Carbon\Carbon; in top of you file):
$minutes = 4;
return Carbon::create(now()->year, now()->month, now()->day, 0, $minutes)->toDateTimeString();
As your integer column duration_temp is in minutes, you have to convert it to seconds before you can get the expected result.
Take your example :
Int = 4 minutes => Int = 4 * 60 = 240 second
To finish :
date ("Y-m-d H:i:s", 240); // will give you 1970-01-01 00:04:00
DateTime accepts extensive Relative Formats. This makes possible as an example:
$durationInMinutes = 67;
$date = date_create('2021-01-01 '.$durationInMinutes.' Minutes');
//or $date = new DateTime('2021-01-01 '.$durationInMinutes.' Minutes');
echo $date->format('Y-m-d H:i:s');
//2021-01-01 01:07:00
Also works correctly with negative minute numbers.

PHP Adding 15 minutes to Time value

I have a form that receives a time value:
$selectedTime = $_REQUEST['time'];
The time is in this format - 9:15:00 - which is 9:15am. I then need to add 15 minutes to this and store that in a separate variable but I'm stumped.
I'm trying to use strtotime without success, e.g.:
$endTime = strtotime("+15 minutes",strtotime($selectedTime)));
but that won't parse.
Your code doesn't work (parse) because you have an extra ) at the end that causes a Parse Error. Count, you have 2 ( and 3 ). It would work fine if you fix that, but strtotime() returns a timestamp, so to get a human readable time use date().
$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);
Get an editor that will syntax highlight and show unmatched parentheses, braces, etc.
To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):
$endTime = strtotime($selectedTime) + 900; //900 = 15 min X 60 sec
Still, the ) is the main issue here.
Though you can do this through PHP's time functions, let me introduce you to PHP's DateTime class, which along with it's related classes, really should be in any PHP developer's toolkit.
// note this will set to today's current date since you are not specifying it in your passed parameter. This probably doesn't matter if you are just going to add time to it.
$datetime = DateTime::createFromFormat('g:i:s', $selectedTime);
$datetime->modify('+15 minutes');
echo $datetime->format('g:i:s');
Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:
!g:i:s 12-hour format without leading zeroes on hour
!G:i:s 12-hour format with leading zeroes
Note the ! item in format. This would set date component to first day in Linux epoch (1-1-1970)
strtotime returns the current timestamp and date is to format timestamp
$date=strtotime(date("h:i:sa"))+900;//15*60=900 seconds
$date=date("h:i:sa",$date);
This will add 15 mins to the current time
To expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval):
(I've also written an alternate form of the below function here.)
// Return adjusted time.
function addMinutesToTime( $time, $plusMinutes ) {
$time = DateTime::createFromFormat( 'g:i:s', $time );
$time->add( new DateInterval( 'PT' . ( (integer) $plusMinutes ) . 'M' ) );
$newTime = $time->format( 'g:i:s' );
return $newTime;
}
$adjustedTime = addMinutesToTime( '9:15:00', 15 );
echo '<h1>Adjusted Time: ' . $adjustedTime . '</h1>' . PHP_EOL . PHP_EOL;
get After 20min time and date
function add_time($time,$plusMinutes){
$endTime = strtotime("+{$plusMinutes} minutes", strtotime($time));
return date('h:i:s', $endTime);
}
20 min ago Date and time
date_default_timezone_set("Asia/Kolkata");
echo add_time(date("Y-m-d h:i:sa"),20);
In one line
$date = date('h:i:s',strtotime("+10 minutes"));
You can use below code also.It quite simple.
$selectedTime = "9:15:00";
echo date('h:i:s',strtotime($selectedTime . ' +15 minutes'));
Current date and time
$current_date_time = date('Y-m-d H:i:s');
15 min ago Date and time
$newTime = date("Y-m-d H:i:s",strtotime("+15 minutes", strtotime($current_date)));
Quite easy
$timestring = '09:15:00';
echo date('h:i:s', strtotime($timestring) + (15 * 60));

subtract time away from a php date time

I have a report I built but the problem is the datetimes in the database for the 3 major events are the same as the system processes then so fast, there is no easy way about it as I aggregate data from 4 servers into one jquery datatable and sort by date time decending.
So my question is how can I take a variable in PHP (string of mysql format date time), and reduce it by 1 second?
dognose answer is fine. Find below a method using DateTime.
For those who are not too confident about strtotime :-)
$string = "2013-06-26 18:00:00";
$date = DateTime::createFromFormat('Y-m-d H:i:s', $string);
$date->sub(new DateInterval('PT1S'));//substract 1 sec
echo $date->format('Y-m-d H:i:s'); //print : 2013-06-26 17:59:59
Doc about "PT1S" here (this can be read as Period Time 1 second)
use date along with strtotime should do the trick:
http://php.net/manual/de/function.strtotime.php
$string = "2013-06-26 18:00:00"; //can have any (valid) format
$subSeconds = 1;
$date = date("Y-m-d H:i:s", strtotime($string . " - {$subSeconds} second"));
echo $date."<br />"; //2013-06-26 17:59:59

UTC time to time based by timezone offset

I am saving all the times in MySQL in UTC(0), so I could change them later while I'm showing the times for the users with different time zones, for saving into db, I use:
function get_utc(){
date_default_timezone_set('UTC');
return date("Y-m-d H:i:s");
}
$now = get_utc();
And now I want to convert those times into different timezones based by timezones offset, I am using this function:
function utc_and_timezone($utc_time, $offset) {
return date("Y-m-d H:i:s", strtotime($offset, strtotime($utc_time)));
}
So for example, if the UTC time is: 2013-01-30 21:06:29
Applying the +5 timezone on that time is easy to find:
$new_time = utc_and_timezone("2013-01-30 21:06:29", "+5 hours"); // Works Fine
It works JUST fine with offsets like 5,6 or other integers, BUT with some other like +3.5, +2.5 this is not working:
$new_time = utc_and_timezone("2013-01-30 21:06:29", "+5.5 hours"); // NOT WORKING
Anyone knows why?!
Any better solutions for making UTC times in different timezones...?
Thanks
SHORT QUESTION:
I want to show a UTC time like 2013-01-30 21:06:29 in +3.5 timezone, how is that possible?
On the PHP side, you can try:
$utc_time = '2013-01-30 21:06:29';
$offset = '3.5';
echo date("Y-m-d H:i:s", strtotime($utc_time) + (3600 * $offset) );
//Returns 2013-01-31 00:36:29
$utc_time = '2013-01-30 21:06:29';
$offset = '-5.5';
echo date("Y-m-d H:i:s", strtotime($utc_time) + (3600 * $offset) );
//Returns 2013-01-30 15:36:29
On the MySQL side, you can just use CONVERT_TZ:
SELECT CONVERT_TZ('2013-01-30 21:06:29','+00:00','+03:30');
//Returns January, 31 2013 00:56:29+0000
SELECT CONVERT_TZ('2013-01-30 21:06:29','+00:00','-05:50');
//Returns January, 30 2013 15:16:29+0000

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

Categories