Get openssl certificate expiration date - php

I've parsed openssl certificate by openssl_x509_parse() function and got an array as a result.
Now I need to get the expiration time of that certificate. In parsed array I have validTo_time_t element which contains a valid unix timestamp. But how to determine what timezone this timestamp belongs for?
So I can't get the real expiration time because that timestamp because it means deifferent dates on different timezones.

php formats this field using it's default timezone. you can get it using http://docs.php.net/date_default_timezone_get function
and once you know the timezone you can convert it to UTC or whatever you need

Unix TimeStamp have no timezone.
It's defined as number of seconds since January 1st, 1970 at UTC.
It's not a defined as a real date but just a bunch of seconds.
No timezone = no problems.
This is why it's a perfect measure of time across different server and different regions. You can just store unix timestamp, and when you convert it to a date it will use your local timezone to determinate the date, this is completly automatic and this means that when you convert givin a different timezone it will convert to the correct date. Always.
This is true for all timestamp not just for the SSL output.
Major info on Timestamp http://www.unixtimestamp.com/

Related

What kind of data time format and how to process this in php 2021-07-09T14:09:47.529751-04:00

I used some api which gets the expiry date as
2021-07-09T14:09:47.529751-04:00
How do I process this and store it in the database.
Do I store this as datetime ?
Does it have any timezone associated with it ?
Is this correct but what do I do with the time zone. My server has a different time zone so if I store this value it would not be correct ?
echo date( "Y-m-d H:i:s", strtotime("2021-07-09T14:09:47.529751-04:00") );
The date format you're looking at is ISO 8601.
Strtotime() will convert it into an unix timestamp that is in UTC and you can save it to your database as a number. The number should be larger than 4 bytes so that you don't run in the Year 2038 problem. Everytime you read the value, you must convert it to the proper timezone of the user. This is the easy way. You'll never have to manually fix the dates in the database.
If you want to save it to database as a date, you probably want to still save the date as UTC and apply any transformations when you show the value. Convert it with gmdate() before saving:
echo gmdate( "Y-m-d H:i:s", strtotime("2021-07-09T14:09:47.529751-04:00") );
If you always show the value in one time zone, you can set the timezone in php.ini. This is not the best way though. It's best make it clear what timezone your program is using and explicitly set it in code.
When you want to show the value in other timezones, refer to this question or the manual of settimezone.

OpenWeatherMap API - Dates in XML

Anyone can explain to me in which format are these DateTime strings from OpenWeatherMap API and how to work with them in order to:
Just see the time (Greenwich +1)
sun rise="2019-01-28T06:31:03" set="2019-01-28T16:14:39"
View the Date & Time (Greenwich +1)
lastupdate value="2019-01-28T11:20:00"
Those dates are in ISO 8601
The examples you actually have do NOT contain any TimeZone information so they could be any timezone..
If you work with datetime offsets (that include time zone information) you would typically see something like this
2008-09-15T15:53:00+05:00
The reasone it is formatted in ISO 8601 is so that the date time picker (that is the actual format you use for the value) on your HTML page knows how to interpret the time into your local (machine clock configured time)
Typically if using UTC you will have a date time that looks like
2008-09-15T15:53:00Z
Which is Zulu Time
And as i mentioned already if it does not have any of those it is assumed local time at server that generated it.
Thanks, I was able to convert them by using concatenated date and strtotime functions.
date('H.i',strtotime($sunset_time));
The local time on the server is Greenwich, whereas I would need to add +1 for my time zone (Europe/Rome).
In the end I was able to add +1 hour like this:
date('H.i', strtotime($sunset_time) + 60*60);
Is this a correct way?

How to make conversion from string to timestamp one-to-one?

I'm totally confused with PHP UTC/Local time conversions. Some functions convert time implicitly, some don't. Argh!!!
Say, I have this PHP code:
$timestamp = strtotime("2016-05-13 09:26:30"); //From MySQL date format
//$timestamp returned is 1463153190
Then if I convert 1463153190 to UTC time, it becomes 05/13/2016 # 3:26pm (UTC). Why???
How do I get a function to convert time from string to a timestamp one-to-one, without any time-zone shenanigans?
2016-05-13 09:26:30 is an incomplete timestamp, since it's missing the timezone. It could refer to over 24 different "absolute points in time", depending on which timezone it's being interpreted in. 1463153190 is an absolute UNIX timestamp for an absolute point in time. To convert your former timestamp to an absolute point in time, it needs to be interpreted as some timezone. The conversion the other way around also needs some timezone to format the point in time in.
In PHP the default timezone is considered when converting to or from UNIX timestamps. You can set that using date_default_timezone_set.
You see what you're seeing because date_default_timezone is set to some timezone (~+6) when converting from the human readable format to the UNIX timestamp, but then you're converting it back explicitly using UTC. Your initial timezone was simply not UTC/the timestamp was not initially interpreted as being in UTC. There's a timezone mismatch between the initial value and the final value, hence an offset of 6 hours.

Formatting MySQL / Server timestamp with php?

I have a created a timestamp in MySQL that changes when an account is updated by a users, and this timestamp is echoed on the page. However it is displaying the server time rather than my local time. I can't set the timezone in MySQL, I tried. What is another way to change this, and how can it be implemented?
The problem with timestamp datatype is that
MySQL converts TIMESTAMP values from the current time zone to UTC for
storage, and back from UTC to the current time zone for retrieval.
(This does not occur for other types such as DATETIME.) By default,
the current time zone for each connection is the server's time. The
time zone can be set on a per-connection basis. As long as the time
zone setting remains constant, you get back the same value you store.
If you store a TIMESTAMP value, and then change the time zone and
retrieve the value, the retrieved value is different from the value
you stored.
So, it does not really matter what your timezone setting in php is, you need to set either mysql's timezone on a session basis using
SET time_zone = timezone
command, or you need to store the timezone of the client along with the timestamp and adjus the value based on the stored timezone. I would use the latter approach, since a client technically can change timezones and if the client access the timestamp data from a different timezone, then different data will be returned by mysql.
When echoing from a database add in some "filters" seemed to do the trick
<?php echo $row['field name']; strtotime(date("Y-m-d", 1310571061)); ?>
Without the above code it displayed the timestamp as: 2016-02-09 00:00:00
With the above code it displays thus: Tuesday, February 09, 2016

What is the difference between these two date?

I have two date:
2014-12-01T12:05:59Z //dont know which format
And
2014-12-01T03:59:00-08:00 //i'll get it from date("c") function in php (ISO 8601).
So, I don't know what is the different in this date's?
And how to convert date in 2014-12-01T12:05:59Z this formate in php?
[UPDATE]
I want to get current timestamps in in 2014-12-01T12:05:59Z this date formate.
It's the ISO 8601 format and both time zone variants are allowed and must be expected. -08:00 means 8 hours behind UTC, Z means UTC and is a shortcut for +00:00.
Both the dates you have mentioned are dates with timezone. The only difference is that the first date has 'z' at the end which is a Zone Designator for UTC and the later date is suffixed with timezone offset, which is generally represented as "HH:MM". Practically, both dates are same and both the representations are correct.
The 'z' suffixed representation is generally accepted xml standard for timestamp in API payloads.
Looking at your requirements, since your API provider needs the date with Zone Designator, they must be calculating time in UTC.
I will suggest changing your timezone to UTC first using
date_default_timezone_set('UTC');
and then use this expression to get required timestamp
date('Y-m-d\TH:i:s\Z', time());
This way you are saved from the timezone conflict and you will also be able to send the required date to the API.
date_default_timezone_set('UTC');
echo gmdate('Y-m-d\TH:i:s\Z');
Check the link. You will get the difference as well.

Categories