strtotime compatibility - php

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)

Related

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.

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

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();

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

PHP converting WIM date format

I am writing a wrapper library for WIM files (long story), but I have been having difficulty with the dates encoded in it. The format is:
[creationtime] => Array
(
[highpart] => 0x01CA0446
[lowpart] => 0x8E44DCAF
)
This format is inside the WIM file XML.
I have tried every date decoding technique I can think of!
Any ideas would be much appreciated.
The only things I have to go on are things related to Long Integers and WIN32 DateTime... But no luck so far.
liCreationTime is defined as LARGE_INTEGER in the WIM spec (I downloaded from MS)
This looked to me like it would be similar (if not the same) as a timestamp returned by QueryPerformanceCounter - which thankfully makes things very simple!
I did this using a calculator and command line PHP, you'll have to implement the code yourself.
Take your high and low parts and turn them into a single 64bit value.
0x01CA04468E44DCAF -> 128920240414579887
Divide that by 100000000
1289202404
Use it as the input to date()
php -r "var_dump(date('Y-m-d H:i:s', 1289202404));"
string(19) "2010-11-08 07:46:44"
Edit:
Or maybe I just got lucky.
If it is the number of nanoseconds since 1601, as other answers seem to indicate, you can use a slightly different calculation.
php -r "var_dump(date('Y-m-d H:i:s', (128920240414579887/100000000)-41651963));"
string(19) "2009-07-14 05:47:21"
I believe they might be something to do with the c/c++ FILETIME structure
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx

Which PHP version is required for str_split?

I relogin to my server in dreamhost and test some scripts.And I found I couldn't use str_split. Message of Undefined function was given.I checked the version in the server and its PHP Version is 5.2.12.And I just wonder which version is required?Thanks.
Testcode:
<?php
$arr = str_split("lsdjflsdjflsdjflsdjfl");
print_r($arr);
?>
Message:
Fatal error: Call to undefined function: str_split() in /test.php on line 3
Edit #Justin Johnson
I checked the server's system directory,and I found there are two versions of PHP in Dreamhost.In user's webroot,file will be parsed by PHP5 and that's why I got php 5.2.12 by putting a phpinfo.php in the webroot.And if php files are ran in command line directly using php test.php,another php version which is 4.x worked.That's the reason I got an error.When I use
/usr/local/php5/bin/php test.php
Everything is fine.
Rather than use str_split, it's usually much easier to iterate through the characters of the string directly:
$s="abc";
$i=0;
while(isset($s[$i])) {
echo $s[$i++]." ";
}
see?
First off: The PHP documentation will always say what version is required for every function on that function's documentation page directly under the function name.
It is possible that an .htaccess file is somewhere in your path and is causing a previous version (<5) of PHP to be used. To double (or triple) check to make sure that you are running in the proper PHP version, place this code above the line where you call str_split
echo "version:", phpversion(),
"<br/>\nstr_split exists? ",
function_exists("str_split") ? "true" : "false";
However, as shown by Col. Shrapnel, it is not necessary to convert a string to an array of individual characters in order to iterate over the characters of that string. Strings can also be iterated over using traditional iteration methods, thus making the call to str_split unnecessary and wasteful (unless you need to segment the string into fixed length chunks, e.g.: str_split($s, 3))
foreach ( str_split($s) as $c ) {
// do something with character $c
}
can be replaced by
$s = "lsdjflsdjflsdjflsdjfl";
for ( $i=0; isset($s[$i]); ++$i ) {
// do something with character $s[$i]." ";
}
which is equally, if not more clear.
According to dreamhost wiki, you need to switch to php5 manually from control panel, if you created your domain before 2008 sept.
http://wiki.dreamhost.com/Installing_PHP5#Using_DreamHost.27s_PHP_5
PHP 5 was added to all plans by
DreamHost as of June 2005. As of
September 2008, support for PHP4 was
discontinued, so you can no longer
switch back to PHP 4 from PHP 5 from
the panel.
If you haven't switched to PHP 5 yet,
you can do this in the Control Panel.
But, again, you will not be able to
switch back to PHP 4 after switching
to PHP 5.
Here's how to switch from PHP 4 to PHP
5:
Log into the DreamHost Control Panel.
Click Domains, then Manage Domains.
Click the wrench icon next to the domain you want to activate PHP 5
on (under the Web Hosting column).
Select PHP 5.x.x from the dropdown menu.
Click Change fully hosted settings now! at the bottom of the
section.
Repeat steps 3-5 for each additional domain you want to
activate.
you could also check your php version with
<?php
phpinfo();
?>
The version required is PHP 5 or later. So theoretically your program should work.
If you can't get str_split to work, just use a string as an array:
$stuff = "abcdefghijkl";
echo $stuff[3];
will produce
d
This method is fastest, anyway. I don't know if it suits your needs, but if it does, I hope it helps!
Could be anything in your code. How do we know its not a 10 line script or 2000 line script?
You can use preg_split() to split an array into single characters, but it will return an extra empty string at the begining and the end.
$a = preg_split("//","abcdefg");
echo json_encode($a);
prints:
["","a","b","c","d","e","f","g",""]

Categories