0

Best of Web #8

best of web - runde 8And again a Monday. Besides the fact that Monday, as the beginning of the week, probably is one of the scariest days of the week, there’s a second thing that always takes place on Mondays: My “Best of Web” series.

Today, it is already the 8th episode and this time there is again a novelty. There is no video in addition to some interesting articles at the end this time, like every week, because nothing suitable has crossed my paths this week.

But everything remains the same. That means – there are a few more bits of web development, the C# world and of the subject of hardware hackers. And now, I hope you enjoy reading.

HDMI over IP Windows client

HDMI over IP PC ClientDaniel was looking for a way to capture videos from HDMI sources with his PC. Since he had had to realize that HDMI capture cards were too expensive, he decided on the following HDMI over IP adapter. Adapters such as this one take a HDMI source, convert the frames into IP packets and send them over the network to a matching receiver. However, instead of using the matching receiver, Daniel wanted to use his PC and, in particular, its network card. So he analyzed the protocol of the HDMI over IP adapter by using Wireshark and then he wrote a client software for the PC.

This kind of project really gives me inspiration and so I’m just dreaming about a project working the other way aound at the moment. So, for example, bringing the image of the PC via LAN to a receiver, which then brings it to the TV via HDMI. So you could take the loud media server in the basement and stream its image over the network.

Cookies without Cookies and Javascript

Luc, a Dutch programmer and security analyst, describes on his website how to track visitors even when Javascript, Flash, cookies and Local Storage are blocked. For this method neither the IP of the visitor nor its user-agent string are needed. I admit that, at first, I could not imagine how that should work, but his shown workaround  is simple and awesome at the same time. I only reveal so much – for tracking, Luc uses only one image file. No more and no less. The solution for the “puzzle” is on Luc’s website.

Dynamic favicons with favico.js

favico.jsWe just talked about a code that works without Javascript, Flash and all the fancy stuff, but in opposite to this, the following subject is based almost exclusively on Javascript. It’s about favico.js, a Javascript library that allows you to create dynamic favicons. So by favico.js you can dynamically change favicons, provide them with small badges or even play animated favicons.

One use case would be to show small badges on the favicon to realize a notification system, so you will immediately know about news/messages, even if the appropriate tab is not in the foreground. All in all, the project is still slightly buggy and not 100% compatible with all browsers. Still, I think Miroslav Magda’s favicon library “favico.js” is worth mentioning.

What does the ??-operator in C# do?

Attention dear friends of C#. Who of you has programmed in C# and has ever used the “??-operator”? And who of you has not yet used it or does not know it? I admit that I have not used the ??-operator, also called null-coalescing operator, too often so far. It is actually a useful enrichment. You can read on code project Sampath Lokuge’s article about the ??-operator and what it does and how und when to use it.

Regex testen im Browser

Regex 101 TesterThat I’m a fan of regular expressions should be clear if you have read more than one or the other article on my blog. (At this point,  the ingenious regex crossword puzzle needs to be mentioned again.) To find the correct regular expression, however, can sometimes be quite tricky and therefore, there are, as for all difficult things, also great tools and helpers.

My both favourite tools are browser-based and you should really check them out. So have a look at myregextester.com an regex101.com.

0

Best of Web #7

best of web - runde 7Today it is once again time for another round of “Best of Web”. This week I have a few more links on the hand, I would like to share with you. Overall, it comes back this week to some technical topics – good for geeks and nerds among you, rather poor for lovers of more “lighter fare”. Among others, today is about compilers, the history of Windows, some web design basics and a bit of image recognition in C#. I hope there is one or the other interesting article for everyone.

As always – who has more link tips, is hereby invited to send me these for one of the rounds of my “Best of Web” series. And now have fun with my recommendations of the week.

Quick introduction to compiler construction

On TechPro Tristan McNab explains in a detailed article the basic principles of compiler construction. As part of his article, the basic fundamentals are explained first and then a small compiler is written step by step. That one can not deal with the entire issue about compiler construction in a single article that should be clear. However in my opinion the article is written in an interesting way and therefore worth reading.

An overview of 30 years of Windows

windows 1.0Over 30 years ago, in November 1983, Bill Gates announced Windows 1.0. Since then, several versions have been released and lots of blue screens passed. On winsource.com Joe Fedewa wrote an overview of all previously released Windows (desktop-)versions with always a screenshot of the respective version and a few additional facts. Easy to digest, nice to see what has changed over the years and therefore also in the Best of the Web roundup for this week.

What technology is behind StackOverflow?

Anyone ever had to do with software and hardware stuff should have heard about Stack Overflow. A lot of questions are asked, answers written and discussions held on the portal and its several divisions pages in every second. Nick Craver, hired as software developer at StackOverflow, wrote an article on his personal blog where he gives an insight on the hardware, which is needed to run such a mammoth project like StackOverflow.

px, rem, em, ex, pt, % – when should I use which?

css measurementsProbably every developer has ever seen measurements in CSS stylesheets, such as px, pt and em. Has written probably. But how many of all the developers actually know when it’s the right time and the right context to use them? Ok, you will get to the goal always, no matter which measurement you use. But there’s still a difference between getting to goal “somehow” or on the “right way”. When to use which unit of measurement, you can read on the design division of tutsplus.com as well as on Dudley Storey’s blog. Both articles are really insightful and a recommendation on my part, if you have to do even remotely with CSS.

Recognize playing cards with webcam in C#

With this link tip we go again into the corner of .NET-developers. In the following article on codeproject.com it is explained how to recognize playing cards by means of a webcam and the Aforge library. To what extent that has a practical use, was once an open question. Much more interesting is the technique and methodology behind it, which certainly can be transferred to some other application areas.

Surfing through the Internet with a Mac Plus

To conclude, there’s another video like every week. In the following clip Jeff, who has once again dusted off his 27-year-old Mac Plus, shows how hes has taken his Mac to the Internet of today. The load times are overwhelming and anyone who thinks their smartphone or PC was too slow, should lead this video to his/her heart first.

0

20 Dropbox tools you should know

When it comes to online storage and sync, then Dropbox is my number 1 and because I’m pretty sure I’m not alone with this sight, below there is a collection of 20 tools which will help you to realize the full potential of your Dropbox.

 

 

dropplets

#1 – dropplets.com / Blog with Dropbox

With dropplets your Dropbox becomes a blogging platform. After the “installation” your Dropbox behaves like a web server.

New articles can be written in the easy to learn Markdown language and published by simply save them in your Dropbox. The whole thing is quick, easy, and also looks quite chic.

 

 

calepin

#2 – calepin.co / Publishing by Dropbox

Calepin.co is the second blog system in our listing. Also Calepin supports the markup language “Markdown” and allows easy publishing of blog posts.

readwrite.com said about Calepin that it’s “the easiest blog platform in the world”.

 

 

dropshorty#3 – DropShorty / Easily share files

DropShorty is a so-called droplet. So a program which which does an action if you drop a file onto the programs icon.

If you pull a file on the DropShorty logo, drop Shorty loads the file into your Dropbox, then creates a sharing url and then shrinks this url by use of a link shortening service of your choice.

 

 

boxcryptor#4 – Boxcryptor / Privacy for your Dropbox

“Secure your cloud” – this is the slogan of the BoxCryptor service. BoxCryptor extends Dropbox by a virtual drive that is synchronized with your Dropbox.

The cool thing about that? – all files will be automatically encrypted before they are synced and uploaded to your Dropbox.

 

 

tunesBag#5 – tunesBag / Media Player for your Dropbox

With tunesBag you can build your own online streaming service out of your Dropbox. Similar to Spotifiy or Google Music, tunesBag will deliver a web interface to you, which enables you to listen to music.

The difference – tunesBag uses the music which you have previously stored in your Dropbox. Supported music formats currently include mp3, m4a, wma and ogg.

 

 

dropbox encore#6 – Dropbox Encore / Use two Dropbox accounts at once

Dropbox Encore is a program for Mac OS X that allows you to use two Dropbox accounts including two synced  folders simultaneously.

This makes sense for example when looking for a solution to use a private and a second account for work.

 

 

dropbox-uploader#7 – Dropbox-Uploader / Shell upload script

Dropbox Uploader is a small bash script, which allows yout to up- and download files from the bash (shell) into your Dropbox account.

This makes, for example, sense if you want to back up files from your web server. Another example would be a CronJob which is conceivable that a folder automatically backs away at specified intervals.

 

 

dropittome#8 – DROPitTOme / Enable anonymous uploads

DROPitTOme allows any web user to upload files anonymous into your Dropbox. Anonymous in this case means that the uploader does not need to know your credentials, not that he or she is 100% anonymous.

After DROPitTOme is connected to the own Dropbox, DROPitTOme creates a custom upload page, where anyone who know the url can upload files into the linked Dropbox. The Dropbox owner may be informed by mail in turn about new uploads.

 

 

dropittome#9 – Wappwolf Automator / Automation in the box

With the Dropbox Automator from Wappwolf you get a tool which can manage a handful of helpful tasks and link services.

So it’s easy to set up, for example, that images after their upload Facebook are automatically saved in the Dropbox too or that text-documents will be automatically converted into PDF format.

 

 

ifttt#10 – ifttt / Dropbox automation, the second

Behind the cryptic name ifttt, which stands for “if this, than that”, there is another automation solution. As the name suggests, the principle is based on action and reaction.

So you can create any number of pairs of rules, such as “If an email with an attachment comes in, then automatically save the attachment in my Dropbox”.

 

 

urldroplet#11 – URLDroplet / Copy files from URL to Dropbox

URLDroplet is a service that allows you to load arbitrary files directly into your own Dropbox, without these being previously cached on your own computer. Therefore you have to enter the URL of a desired file and then URLDroplet copies this file into your Dropbox.

 

 

droppages#12 – DropPages / Dropbox as a CMS

DropPages is a little like the first two tips in this listing. So DropPages also uses your Dropbox to manage and publish a websites.

The difference lies rather in the target orientation. Where link-tip 1 and 2 want to be a blogging platform, DropPages wants to be a “classic homepage CMS”.

 

 

gimmebar#13 – Gimme Bar / Offline bookmark service

Gimme Bar is the “little different bookmarking service”. Instead of simply storing links to the content and organize them, Gimme Bar stores the whole website/content in your Dropbox.

Once synchronized with the home PC, one has his favorite web content offline available.

 

 

Send to Dropbox#14 – Send to Dropbox / Via mail to the Cloud

After sign-in at “Send to Dropbox” you obtain a personalized email address. Now if you send an e-mail with an attachment to this address, the attachments are automatically stored into your Dropbox by the Send to Dropbox servers.

So you can put your files from any device which supports mailing into your Dropbox in no time.

 

 

droptunes#15 – DropTunes / Media player with iTunes look

Drop Tunes is another media player which takes Dropbox as a source for the music files. By design, the player looks really similar to iTunes, and is available both as an iOS app as well as a web application.

 

 

Dropbox Uploader#16 – Dropbox Uploader / PHP to Dropbox upload script

Dropbox Uploader is a PHP class that allows you to upload files to your Dropbox account by using the PHP scripting language.

So if you are looking for a opportunity to connect the Dropbox service in a Web project, you should have a look at the Dropbox Uploader class.

 

 

scriptogram

#17 – Scriptogr.am / Minimal blogging

Scriptogr.am is another platform, which makes blogging possible by using your Dropbox account. Here scriptogr.am focuses not only on simplicity, but also has a minimalist layout and clear design language.

The special features include the presence of a theme for mobile devices and the possibility to connect your own (top-level) domains onto your scriptogr.am blog.

 

 

pancakeio#18 – pancake.io / Simple web-publishing

By means of pancake.io you can publish plaintext as also html documents to the web. In contrast to the already introduced blogging solutions, pancake.io doesn’t try to be a CMS and/or a complete blogging system, but much more of a simple platform to offer you an easy to handle tool to publish individual files and documents quickly.

 

 

writebox#19 – Writebox / The text editor for Dropbox

WriteBox is a text editor for Dropbox. While using the minimalist interface of WriteBox, you can open, edit and save directly documents directly from your Dropbox.

The interface focuses on the essentials – the text. Quite nice – key combinations such as Ctrl+S for saving still work, even if Writebox is a webapplication.

 

 

dropbox print#20 Labnol Dropbox Print / Print from everywhere

By means of the tutorial and a small script from labnol, a folder named “Print” is created inside your Dropbox folder. If you save a filel into this folder, no matter from where and from which device, the document will be printed on your local printer.

0

Benchmark: strtotime() vs DateTime vs getTimestamp in PHP

Recently I wrote about how one can implement date comparison in PHP. In the article I presented two approaches. Firstly, using the strtotime() method and another with the DateTime class.

Then, in the comments on the german division of this blog, it was pointed out that the strtotime() variant is probably faster. Because I wasn’t sure about this, I decided to make a small performance test and share the results with you in this article.

How did I test the performance?

Since the question aims on strtotime() vs. DateTime, the DateTime class is contrary to the strtotime() function. So because DateTime class offers more functionality, I had first to create the DateTime object and then get the timestamp of it, because the strtotime function gives a timestamp as return value and I wanted to compare the time needed to generate the identical output.

As input I have taken a date without a custom time – say a year, month and day indication. In addition, I tested a third function. What exactly is going on with the getTimestamp () function, I will explain later in the article. The three function calls for my following benchmark look as follows:

//strtotime()
$a=strtotime("2012-05-28");

//DateTime
$date=new DateTime("2012-05-28");
$a = $date->getTimestamp();

//getTimestamp()
$a = getTimestamp("2012-05-28");

How did I measure?

As a reference value I wanted to specify the time in seconds. To measure time, I used the following functions:

//Time prior to the execution of a variant
$startTime = microtime(true);

/***************************
  Run variant / tests
****************************/

//Time after the end of the test
$endTime = microtime(true);
//Period of the test
$elapsedTime = abs($startTime - $endTime);

Each of the functions has been called in a loop for 100000 times while the time was measured. To compensate for fluctuations and measurement inaccuracies, the measurement for each variant was performed 20 times and the mean/average “elapsed time” of 20 measurements was taken. Finally the code for my benchmark looked as follows:

$total = 0;
for ($runs = 0; $runs < 20; $runs++)
{
    $startTime = microtime(true);
    for ($i = 0; $i < 100000; $i++)
    {
        //Execute function / variant
    }
    $endTime = microtime(true);
    $total += abs($startTime - $endTime);
}
$elapsedTime = sprintf("%.4f sec.", ($total/20));

I also have added the following line of code at the beginning of the benchmark scripts to set the correct time zone for the strtotime()-function as well as for the DateTime variant.

date_default_timezone_set("Europe/Berlin");

I have written and run the test code in WebMatrix 2 via its supplied IIS Express Web server. The hardware of the test environment was as follows:

  • Acer Aspire 5740G (Notebook)
  • Intel Core i3 M 330 @ 2.13Ghz
  • 8GB Ram
  • Windows 8 Pro (64-Bit)

What is the result?

The result ended up so as it had already suspected by Christoph. The DateTime variant was behind the strtotime() function. Second place went to the getTimestamp () function. In initial tests, this cut off so good that made it to the first place. Here, however, still lacked some calculations, which have finally catapulted getTimestamp() to second place.

I have summarized the whole thing in two small graphics.

php_date_to_timestamp_benchmark1

php_date_to_timestamp_benchmark_percent

What’s up with the getTimestamp()?

The getTimestamp () function is not an official PHP function, but a small in-house development from me. After doing a few tests with strtotime() – which had out performed the DateTime version, I wanted to see if I myself can create a faster version.

As can be seen from the above graphs, mine function was not quite successful. In initial tests it had indeed worked, but I was made aware in the comments that my getTimestamp() function would not always return the correct values.  After I have added the missing switching between summer and winter time (for our room – in America will be converted on other dates!), my function is now a little bit slower than the strtotime() function.

My getTimestamp function looks like this:

function getTimestamp($timeStr)
{
    $daysInMonth = array(0,31,59,90,120,151,181,212,243,273,304,334);
    $dateArr = explode("-", $timeStr);

    //Add years to the day of the year entered
    $days = ($dateArr[0]-1970) * 365;

    //Add leap years, days
    for ($i = 1972; $i <= $dateArr[0]; $i+=4)
    {
        if (($i%4==0 && $i%100 != 0) || $i%400==0)
            $days++;
    }

    //Days until 31.03. of the year
    $daysTimeswitch = $days + 90;
    //Determine day of the week using MOD 3 . (+3 because 01.01.1970 = Thursday)
    //Pull off day of the week from 31. = last Sunday of the month => Daylight saving time
    $daySummertime = 31 - ((($daysTimeswitch%7) + 3)%7);

    $daysTimeswitch = $days + 304;
    $dayWintertime = 31 - ((($daysTimeswitch%7) + 3)%7);

    if (($dateArr[1] > 3 && $dateArr[1] < 10)
        || ($dateArr[1] == 3 && $dateArr[2] > $daySummertime)
        || ($dateArr[1] == 10 && $dateArr[2] <= $dayWintertime))
    {
         $summerWinterTime = 3600;
    }
    else{
         $summerWinterTime = 0;
    }

    //Pull off leap year day
    $days -= (($dateArr[0]%4==0 &&  $dateArr[0]%100 != 0) ||  $dateArr[0]%400==0) ? ($dateArr[1] > 2) ? 0 : 1 : 0;

    //Add days to months of the current year
    $days += $daysInMonth[$dateArr[1]-1];

    //Add days of the current year
    $days += $dateArr[2] - 1;

    //Pull off 3600 seconds for time shift. (Unix timestamp / UTC)
    return $days * 86400 - 3600 - $summerWinterTime;
}

Anyone who has attentive read the source code of the above function, perhaps wants to scream “Hold on …”. Yes, correctly I have to admit that my function gives correct values only under certain circumstances.  Following requirements must be met:

  • The date must be passed in the form YYYY-MM-DD
  • The date should not be older than the 01/01/1970

The strtotime() function takes also data in other formats and can calculate to a certain extent also timestamps of data before 01/01/1970. Here my function is clearly at a disadvantage and you might be wondering whether the comparison then may be unjustified. However, one must also note that the DateTime class in turn provides greater functionality than the strtotime() function. So this would also be unjustified…

And since in the benchmark it only was about the generation of timestamps from a given date, I think that my function has its right to exist. To be a stickler for details – the Unix timestamp begins on 01/01/1970 and thus my function meets the basic requirements for calculating a Unix timestamp.

Conclusion

Claiming strtotime() is faster than DateTime could be assigned by the test. In addition, I was able to determine that I’m not able to write a faster function than strtotime(). At the beginning of the tests, I thought that if I would limit myself to the minimum, I could write a faster function. Finally, the strtotime() function has a few more  options than my getTimestamp() function and however, it did not succeed.

Ultimately, the choice of method should also always be taken in the context of the project. What format is you input data? Do I perhaps like to calculate more than just the timestamp?

Finally, a question to you – the readers: Have any of you ever tried to write a function that is faster than strtotime()? And if so, which way did you go and what problems did you encounter?

1

How to compare dates in PHP

php_date_compareHow to actually compare 2 dates in PHP with each other? The question sounds easier than it is.  The first thought that usually comes is the following. We store both dates as strings and compare them.

Preliminary

<?php
$date1 = "2012-1-12";
$date2 = "2011-10-12";

if ($date1 > $date2)
 echo "$date1 is newer than $date2";
else
 echo "$date1 is older than $date2";
?>

Output:

2012-1-12 is newer than 2011-10-12

At first glance this seems to be a workable solution. But what if the two data is in a different format?

<?php
$date1 = "12-1-12";
$date2 = "2011-10-12";

if ($date1 > $date2)
 echo "$date1 is newer than $date2";
else
 echo "$date1 is older than $date2";
?>

Output:

12-1-12 is older than 2011-10-12

Now the date in 2012 is declared to be older than the date in 2011, which is of course wrong. However, from PHP’s point of view, this behavior is correct, because two strings were compared and so the 2012 date string was definitely the smaller/shorter string.

How to properly compare dates in PHP

A working solution would be to first convert the date into timestamp format and then to compare these numerical timestamps each other. To convert a date string into a timestamp, PHP provides the function strtotime ($time_string), which accepts a date or a time value and returns the corresponding timestamp.

<?php
$date1 = "12-1-12";
$date2 = "2011-10-12";
$dateTimestamp1 = strtotime($date1);
$dateTimestamp2 = strtotime($date2);

if ($dateTimestamp1 > $dateTimestamp2)
 echo "$date1 is newer than $date2";
else
 echo "$date1 is older than $date2";
?>

Output:

12-1-12 is newer than 2011-10-12

However, well known, many roads lead to Rome, so there are still other solutions for the initial problem. So one of these alternative solutions would be to use the DateTime class that is provided by PHP as of version 5.2.0.

<?php
$date1 = new DateTime("12-1-12");
$date2 = new DateTime("2011-10-12");

if ($date1 > $date2)
 echo $date1->format("Y-m-d")." is newer than ".$date2->format("Y-m-d");
else
 echo $date1->format("Y-m-d")." is older than ".$date2->format("Y-m-d");
?>

Output:

2012-01-12 is newer than 2011-10-12

How do you compare two dates in PHP? Do you use one of the two identified ways or is it another? I would be interested to know how you solve yourself the problem.

Page 1 of 27