DateTimeZone works with PHP v7.0 but not with v5.4 - php

While converting from one timezone to another the following codes works well with PHP v7.0(dev) but throws an error when executed in PHP v5.4. How can I make it compatible?
//---CODE------------------------------------------------------------
$time_object = new DateTime('2011-04-19 17:45', new DateTimeZone('+0530'));
$time_object->setTimezone(new DateTimeZone('Europe/London'));
$LondonDateTime = $time_object->format('Y-m-d H:i:s');
echo $LondonDateTime;
Error in PHP v5.4
//---ERROR WITH PHP v5.4--------------------------------------
DateTimeZone::__construct(): Unknown or bad timezone (+0530)

It's down to this PHP bug, which was fixed in PHP 5.5.10. You should be able to use this (admittedly quite ugly) workaround:
DateTime::createFromFormat('O', '+05:30')->getTimezone();

Related

phpSPO and passing null to strlen

I need to get a file from SharePoint using phpSPO.
I use this code:
$authCtx = new AuthenticationContext('https://xxxxxxxx.sharepoint.com');
$authCtx->acquireTokenForUser($username,$password);
$ctx = new ClientContext('https://xxxxxxxx.sharepoint.com/sites',$authCtx);
require('settings.php');
$sourceFileUrl = "/path/to/excel.xlsx";
$targetPath = "/files/excel.xlsx";
$fileContent = Office365\SharePoint\File::openBinary($ctx, $sourceFileUrl);
But I get this error:
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/www.mydomain.dk/vendor/vgrem/php-spo/src/Runtime/Http/Requests.php on line 137
Isn't phpSPO ready for php 8.0 or what could be wrong?
A deprecation is not an error. It's a hint to the author of the code that they will need to fix the code "soon" (i.e. within the next few years, when PHP 9.0 is expected to ship) so that it doesn't become an error.
If you want to identify what needs changing and contribute a fix, I'm sure the maintainer will appreciate the help.
If not, it is perfectly safe to completely ignore the message. If necessary, change your error configuration to ignore deprecations, or to ignore them within the "vendor" directory where third-party code lives, using something similar to this answer.

Why PHP COM objects are not returning correct results?

I'm trying to access Windows SAPI5 or Text to speech (TTS) using PHP. The standard approach is to create a COM object for "SAPI.SpVoice", then get the installed voices.
Sample PHP code:
<?php
$obj = new COM('SAPI.SpVoice');
$voices = $obj->GetVoices;
$count = $voices->Count;
print $count; #prints "1"
Unfortunately the output returned from PHP's COM object is incorrect because I have 5 voices installed on my system, but PHP only returns 1.
So, just to check if this a PHP specific issue, I wrote the same code in Perl 5.8 (strawberry).
Sample Perl code:
#!/usr/bin/perl
use Win32::OLE;
my $obj = Win32::OLE->new('SAPI.SpVoice');
my $voices = $obj->GetVoices;
my $count = $voices->Count;
print $count; #print "5" which is correct.
So the perl code correctly returns that I have 5 TTS voices on my system, but PHP returns only 1?
Is this a bug or am I doing something wrong? What could be the possible cause of this?
P.S. I've tried this on two different computers and results are the same.
I figured this after some trial error. It looks like if I use the 32-bit version of PHP then I get the correct results (5 voices). But since I had installed the 64-bit version by default I only get 1 voice.
I think the TTS voices are mostly 32 bit (like those installed on my system) and so when running with a 64-bit php.exe it only returns 64-bit voices. With 32 php.exe it returns all voices.
Posting this as answer in case someone faces a similar issue in future.

php 5.4 charset for Exception

In php 5.4 my code dont work properly. I use cyrillic charset. In short:
throw new Exception('Сообщение');
will output:
Fatal error: in test.php ...
although the result would be:
Fatal error: Uncaught exception 'Exception' with message ...
If I dont use cyrillic characters, the result is Ok. Moreover, if I run this code in 5.3, I'll get the proper result. I. e. if I use cyrillic, the result message is empty string.
There are reported issues with non utf-8 chars in exceptions. Try converting the message to utf-8 like so:
throw new Exception(utf8_encode('Сообщение'));
if that does not work then try the following:
$message = 'Сообщение';
$message = mb_convert_encoding($message, 'Windows-1251', 'UTF-8');
throw new Exception($message);
-- EDIT --
The actual problem is not that the exception message is not stored, but rather - the exception is not displayed properly. In PHP 5.3, xdebug is not turned on by default and in PHP 5.4, it is. xdebug is set to display everything in UTF-8 and your message is probably encoded in some other charset, thus the message not being rendered correctly.
If you scroll to the bottom of this page, you will find a single comment referring to this problem.
PHP themselves tracked this issue on here
This stackoverflow thread is also related to the same issue.
You might be able to get away by setting the xdebug encoding to a non utf-8 charset. Please read the xdebug manual regarding this

strtotime compatibility

Ok this relates to another question I have asked but as I have now pinpointed
the issue I thought it would be clearer on a new question.
The problem I am having relates to strtotime under PHP 4.4
I have a larger script that is failing under PHP 4.4 but works under PHP 5+
To check it was this I wrote the following and copied it to two servers one
running PHP 4.4 and the other PHP 5.3.
<?php
$my_time = '2013-03-18T21:38:58.000Z';
$my_time = strtotime($my_time);
echo $my_time;
?>
The output under PHP 4.4 was: -1
The output under PHP 5.3 was: 1363642738
Is there a way to get the same result under PHP 4.4 as 5.3??
Thanks in advance for any help!
-1 was the failure code for PHP < 5.1. This makes me think that PHP 4 can't parse that string.
This means you'll have to use a format both versions support. As a hack, you could do some string manipulation. If you can change the format of the string though, that's your best route. (Actually, your best route is to run the hell away from PHP 4.)
I don't have an installation of PHP 4 to test with, but try removing the .000Z from the end, all the examples on the strtotime page on PHP.net don't have that bit PHP 4.4 may not support it.
If that works ultimately you can parse any time stamps with a substr($my_time, 0, 19)

Using imagick montageImage PHP

I'd like to use imagemagick to make a montage from existing photos on my server. I am having a terrible time getting any code to work properly. I've tried using the few examples I've found by searching, but it's not working.
http://us1.php.net/imagick outlines the library beautifully, but there are no montage examples. I'd just need a proper example to get me off on the right foot and I' can usually take it from there.
Does anyone know of a good resource to learn imagick for PHP?
Here is a working example of montageImage. The example does a color analysis of a photo. To make sure it works properly, you will need to make sure:
You have a valid "test.png".
Imagick::clone has been deprecated as of imagick 3.1.0 in favour of using the clone keyword. So if you have imagick 3.1.0, you will need to change lines 13 and 17.
$bright = $average->clone();
to
$bright = clone $average;
and
$dark = $average->clone();
to
$dark = clone $average;
Good luck.
Did you start with the basic example?
http://us1.php.net/manual/en/imagick.examples-1.php

Categories