Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello Everyone,

I have two functions in PHP one returns the microtime and the other one returns the float value of it.

PHP
$time_start = microtime();
or
$time_start = microtime_float();

function microtime_float()
{
	$microtime = microtime(); 
	$components = explode(' ', $microtime); 
	return sprintf('%d%03d', $components[1], $components[0] * 1000); 
}


This time($time_start) is stored in a text file timex.txt, i have to read this time(the text file timex.txt) in C++ and have to delete this file if the time($time_start) is older than 1 minute.

I tried everywhere online to compare times of C++ with php time. How do i achieve this?

In Simple words, i have to store a php time in a file, and compare it with current time in C++ and delete the file if the stored time is older than one minute.
Posted
Updated 28-Jun-12 10:02am
v2

Which type of the operating system you use?
Please provide a lot more detail about this.

If you use the Win, then :

You can use following function of WinAPI

BOOL GetFileTime(
HANDLE hFile,
LPFILETIME lpCreationTime,
LPFILETIME lpLastAccessTime,
LPFILETIME lpLastWriteTime
);

BOOL FileTimeToSystemTime(
const FILETIME* lpFileTime,
LPSYSTEMTIME lpSystemTime
);

If you need a other way,
You can following way for Unix platforms:

#include
#include <unistd.h>
#include <time.h>


struct tm* clock; // create a time structure
struct stat attrib; // create a file attribute structure
stat("afile.txt", &attrib); // get the attributes of afile.txt
clock = gmtime(&(attrib.st_mtime));

where:

struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
Or see here:
http://www.freebsd.org/cgi/man.cgi?query=stat&amp;apropos=0&amp;sektion=2&amp;manpath=FreeBSD+5.3-RELEASE+and+Ports&amp;format=html[^]

Regards,
Alex.
 
Share this answer
 
Comments
amarasat 29-Jun-12 14:08pm    
Thanks alot for the information, i was able to solve my issue by using "pwasser's" piece of code
Volynsky Alex 30-Jun-12 12:54pm    
You are welcome. If you have any questions amarasat, please feel free to ask
amarasat 6-Jul-12 14:17pm    
Hello Alex,

I have a question:

$dir = 'sqlite:C:/Program Files/'.$DataBaseFileName.'.sqlite';
$database = new PDO($dir) or die("cannot open the database");//creates new or opens existing database.

I don't want to create a new database(if it does not exists), but opens if the database exists. What should i do?
Volynsky Alex 7-Jul-12 5:06am    
OK.
Let's see here:
http://www.phpkode.com/source/p/mediawiki/mediawiki-1.16.4/mediawiki-1.16.4/includes/db/DatabaseSqlite.php
Maybe it will help you?
There is an error in you function. Should read:

PHP
return sprintf('%d.%03d', $components[1], $components[0] * 1000); 


Also it does not return a float but a string.

Now to deal with time in seconds read as a string from a file and compared to current time the following may help.

C#
char *floatTime = "1340942352.678";

bool bTest = false;

time_t tNow = 0;

while(!bTest)
{

::time(&tNow);  // Get current time in seconds since epoch.

if((tNow - (time_t) ::atof(floatTime)) > 60)
    bTest = true;
}
 
Share this answer
 
v3
Comments
amarasat 29-Jun-12 14:06pm    
Thanks a lot, you solution has solved my issue!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900