Saturday, October 6, 2012
PHP Get time difference in Hour,Minutes or Seconds.
7:08 PM
Starting from PHP 5.3,it has became so much easy for the programmers to code less and get more because of the in-built classes that came with it.One of from those classes is the DateTime() class which made it lot more easier for using it in many different ways,one of the most required technique from this class is getting the difference of time in Years,months,days,hours or even seconds :).
So lets start, we have a scenario that we want to actually get the difference between a past date,suppose a past date you want to compare it with the current date, initiate the class:
First we need to get the today's time, Year-Month-Date Hour:Minute:Seconds , so in this way we can totally compare the past time and get the actual difference in every possible way :). Next,
Now you can get the years,months,days,hours or minutes and vice versa, by the following method:
So, now you can actually get the total difference in anyway you want,even in seconds.
Comments are welcome, hope you have learnt these small tricks,and in a way they are allot more helpful!
So lets start, we have a scenario that we want to actually get the difference between a past date,suppose a past date you want to compare it with the current date, initiate the class:
$today = new DateTime(date('y-m-d h:m:s'));
First we need to get the today's time, Year-Month-Date Hour:Minute:Seconds , so in this way we can totally compare the past time and get the actual difference in every possible way :). Next,
$pastDate = $today->diff(new DateTime('2012-09-11 10:25:00'));
Next we are going to compare the past time with the current, so using the DateTime()'s method diff() , since $pastDate is a DateInterval object.
echo $pastDate->y; //return the difference in Year(s).
echo $pastDate->m; //return the difference in Month(s).
echo $pastDate->d; //return the difference in Day(s).
echo $pastDate->h; //return the difference in Hour(s).
echo $pastDate->i; //return the difference in Minute(s)
echo $pastDate->s; //return the difference in Second(s).
So, now you can actually get the total difference in anyway you want,even in seconds.
Comments are welcome, hope you have learnt these small tricks,and in a way they are allot more helpful!
♥ Happy Coding! ♥
Thursday, October 4, 2012
PHP Get Process Creation Time.
11:55 PM
So, this is a very quick and dirty method that i am going to tell you today, actually whenever on windows platform if you need to get the time of when is a certain process is created, actually nobody needs it that much only but if you are creating system layered applications with PHP and are going deep inside digging through the WinCOM Objects.
Anyway, i won't recommend this method but may be any of you finds it useful in any scenarios,so here we go:
First of all you need to know, what actually are you trying to achieve,the process in Windows can be accessed using two ways, either by using the PID (Process Identifier) or the name (Image Name). So i will tell you the both ways you can actually get to that by how, here is the code :
So, what is actually happening above, its quite simple, i am using COM objects with PHP, even though you should use them with great precautions because they can be huge security risk if exploited so you should have to be really careful playing around with these.
Just change the $pid = "9252"; to any PID you like,and if you want to know the process creation time using its name,than it is quite simple as well, just change the ProcessId with ProcessName in $wmi->ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId ='".$pid."'"); and you are good to go.
There could be many more methods out but i used this one for my own usage and it quite helped me. But as before i would recommend you to thoroughly tighten the security if you are using COM objects on Enterprise level.
Happy Coding!
Anyway, i won't recommend this method but may be any of you finds it useful in any scenarios,so here we go:
First of all you need to know, what actually are you trying to achieve,the process in Windows can be accessed using two ways, either by using the PID (Process Identifier) or the name (Image Name). So i will tell you the both ways you can actually get to that by how, here is the code :
$pid = "9252";$wmi = new COM('winmgmts://'); $process = $wmi->ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId ='".$pid."'"); $pDate = floor($process->CreationDate); $formatDate = date_parse($pDate);$time = $formatDate['hour'].':'.$formatDate['minute'].':'.$formatDate['second'];print $time;
So, what is actually happening above, its quite simple, i am using COM objects with PHP, even though you should use them with great precautions because they can be huge security risk if exploited so you should have to be really careful playing around with these.
Just change the $pid = "9252"; to any PID you like,and if you want to know the process creation time using its name,than it is quite simple as well, just change the ProcessId with ProcessName in $wmi->ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId ='".$pid."'"); and you are good to go.
There could be many more methods out but i used this one for my own usage and it quite helped me. But as before i would recommend you to thoroughly tighten the security if you are using COM objects on Enterprise level.
Happy Coding!
Wednesday, October 3, 2012
Fix PHP FATAL Memory Error, Allowed Maximum Size of X bytes Exhausted... [Solution]
12:52 PM
![]() |
Today while processing PNG images in one of my script, i came across this error:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 7680 bytes) in image.php on line 33.
So, i thought may be i need to set the memory to somehow really high, as i am using a fairly decent laptop as my development environment with 8 GB of RAM backed by Core i5 SandyBridge processor, so i didn't wasted another second and added the following line to the top of my script :
I know this could be insane,but i was a bit in the flow of coding so i didn't care, amazingly this DIDN'T SOLVED THE PROBLEM. I was totally blank, and i was like, how could this be possible? I mean i know i am processing fairly big enough PNG images which are direct from DSLR but that shouldn't matter, the same images which are same in resolution but in JPEG are working fine. I thought may be this limit isn't working through the script, so i corrected the limit in php.ini. Amazingly, this either didn't solved the problem itself.
ini_set("memory_limit" , "1024M");
After allot of struggle with this, i just tried to relax myself, and said to myself " I need to do something really tricky here!".
So finally i ended up with following line:
ini_set("memory_limit" , "-1");
To my surprise, this solved the issue instantaneously and my script worked even better, so i thought it may have some draw back issues on my CPU usage, but i monitored the CPU and RAM usage and it didn't went that up as i expected. But who cares for these things when you actually achieve your goal.
So, what does this actually do? This actually tells PHP to override all the memory limits and use as much memory as possible, this won't eat that much memory but it will if you use fairly large images, i was actually processing 8MP.
Keep in mind that huge memory limits can crush your server, and those scripts which run frequently and poorly coded can be a serious pain in rear for daily uses as they may be slow/sluggish and can even cause server crash as well, so use this with caution, as i am on development platform so i didn't care about this, but if you are going to use this trick on other platform other than that of development,this might cause you severe problems as well. So code neat, be clear to yourself, avoid unnecessary code blocks and be simple and straight forward for it.At last i would say that, everything you go onto, just give it a try,actually you learn only when you take risks, so its better to try many things, "Practice makes a man perfect.".
Keep this motto in your mind, be positive for yourself and don't worry about what might other think about your coding standards because everyone lives for themselves, so do your code. Comments are welcome, let me know your solutions about this.
This isn't just for image processing but you can use it though at any stage, but first try to allocate the similar sizes of memory like , 32M , 64M, 96M to 256M or even 512M. But make sure to debug your script and know at which stage is it consuming the most of the memory, so you can actually know what is causing it to hog.
♥ Happy Coding ♥
Monday, October 1, 2012
PHP - a Billionaire's Choice.
3:16 PM
PHP which came into existence 17 years ago, in 1995 when Rasmus Lerdorf created a script from Perl known as ''Personal Homepage Tools'' was not the first to the ground for dynamic scripting.
The programming language took height when PHP 4 was initially released in 2000 and than became a star when PHP 5 was introduced in 2004 followed by the powerful Zend Engine.PHP became the choice for programmers rapidly due to its flexibility in coding and speed of parsing and the choice of extensions which totally boosted PHP's ground market and somehow defeating the major Competitors, ASP- Active Server Pages, which was the some-how similar to PHP or you can say PHP was similar to ASP but PHP was developed by using a mixture of different tastes of many languages, like Perl and Python to the C.
Many popular companies choose'd PHP, the major example is Facebook and Yahoo!. Just because of its flexibility and freedom to code.PHP became popular because it was open-source so you can download the source, tweak it according to your needs, remove any bloat stuff, keep the necessary things within and so on.
The cross-platform independent language became so popular that now-a-days its first of the choice by any newbie web designer / developer / programmer, just because its so fun and easy to learn through its every step.
This giant scripting language has the footholds on any operating system,no matter if it is *inx/Windows/Mac , you have the choice, choose Mac and get the PHP-Packages, install it , choose your favorite code editor program and start coding.
Learning PHP is so simple, you have allot of tutors out there who can teach you for free, i mean seriously, every time you face a problem or you need to know "What is this stuff for?" you have a complete bible written over there at www.php.net with full manuals and most of all the comments below every manual helps you understand the basics and advanced techniques as well.
When i first started to learn PHP, i didn't knew HTML properly, seriously, I don't! But wait, i went through www.w3schools.com , it was back in 2005. I learned PHP and at that time i was totally new to coding, and i feel no shame in saying so that i am still in a process of learning,but while in the process of learning i came to know allot of things, the most of all is the Linux's Terminal, Yeah... you read it right, the Linux's Terminal, i learned allot of commands for it while compiling the PHP's source under ubuntu and than fedora.
Anyway, i just created this Blog to share everything i have learnt so far, and i hope so that you guys, if any read this, will surely comment about all this :)
Best.
The programming language took height when PHP 4 was initially released in 2000 and than became a star when PHP 5 was introduced in 2004 followed by the powerful Zend Engine.PHP became the choice for programmers rapidly due to its flexibility in coding and speed of parsing and the choice of extensions which totally boosted PHP's ground market and somehow defeating the major Competitors, ASP- Active Server Pages, which was the some-how similar to PHP or you can say PHP was similar to ASP but PHP was developed by using a mixture of different tastes of many languages, like Perl and Python to the C.
Many popular companies choose'd PHP, the major example is Facebook and Yahoo!. Just because of its flexibility and freedom to code.PHP became popular because it was open-source so you can download the source, tweak it according to your needs, remove any bloat stuff, keep the necessary things within and so on.
The cross-platform independent language became so popular that now-a-days its first of the choice by any newbie web designer / developer / programmer, just because its so fun and easy to learn through its every step.
This giant scripting language has the footholds on any operating system,no matter if it is *inx/Windows/Mac , you have the choice, choose Mac and get the PHP-Packages, install it , choose your favorite code editor program and start coding.
Learning PHP is so simple, you have allot of tutors out there who can teach you for free, i mean seriously, every time you face a problem or you need to know "What is this stuff for?" you have a complete bible written over there at www.php.net with full manuals and most of all the comments below every manual helps you understand the basics and advanced techniques as well.
When i first started to learn PHP, i didn't knew HTML properly, seriously, I don't! But wait, i went through www.w3schools.com , it was back in 2005. I learned PHP and at that time i was totally new to coding, and i feel no shame in saying so that i am still in a process of learning,but while in the process of learning i came to know allot of things, the most of all is the Linux's Terminal, Yeah... you read it right, the Linux's Terminal, i learned allot of commands for it while compiling the PHP's source under ubuntu and than fedora.
Anyway, i just created this Blog to share everything i have learnt so far, and i hope so that you guys, if any read this, will surely comment about all this :)
Best.
Subscribe to:
Posts (Atom)