How to convert a Date to an Unix TimeStamp?

Post Reply
User avatar
vilian
Posts: 984
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

How to convert a Date to an Unix TimeStamp?

Post by vilian »

Hi Guys,

How could I convert a date/time to an Unix TimeStamp?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Ruth
Posts: 172
Joined: Fri Dec 07, 2007 1:26 pm
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by Ruth »

...maybe this ist what you are looking for?
https://forums.fivetechsupport.com/view ... 8b1485830b
kind regards
ruth
User avatar
vilian
Posts: 984
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by vilian »

Ruth,
I tried it, but didn´t work here. IF you compare the result with that generated from https://www.timestamp-converter.com/, they aren´t the same.
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42268
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by Antonio Linares »

We have already implemented those functions for FWH already

Please lets wait for Mr. Rao comments about them
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by nageswaragunupudi »

* New functions: (valtostr.prg)
FW_DateToUnix( dDate/tDateTime ) --> nUnixTimeStamp in MilliSeconds
FW_UnixToDate( nUnixTimeStamp(inMilliSeconds) ) --> tDateTime

Code: Select all | Expand

#ifdef __XHARBOUR__
   #xtranslate HB_STOT( <c> ) => STOT( <c> )
   #xtranslate HB_DateTime()  => DateTime()
#endif



//----------------------------------------------------------------------------//

function FW_DateToUnix( tDateTime )  // ( dDate or tDateTime ) --> nMilliSecs

   DEFAULT tDateTime := HB_DateTime()

return INT( ( FW_DTOT( tDateTime ) - HB_STOT( "19700101000000" ) ) * 86400000.0 )

//----------------------------------------------------------------------------//

function FW_UnixToDate( nMilliSecs ) // --> tDateTime

   if ValType( nMilliSecs ) == "C"
      nMilliSecs := Val( nMilliSecs )
   else
      DEFAULT nMilliSecs := 0
   endif

return HB_STOT( "19700101000000" ) + ( nMilliSecs / 86400000.0 )

//----------------------------------------------------------------------------//
 
Please keep in mind that the FWH functions deal with Unix time in MilliSeconds. Not seconds.
Depending on the requirement, you need to provide the conversion ( *1000 or /1000 )
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by nageswaragunupudi »

vilian wrote:Hi Guys,

How could I convert a date/time to an Unix TimeStamp?
If you want to use this functionality with MySql, you can use the MySql built-in functions

Code: Select all | Expand

UNIX_TIMESTAMP( <datetime> ) 
FROM_UNIXTIM( nUnixTime )
Please note that in this case unix timestamp or in Seconds. Not milli seconds. We need to take care of conversions.

Please note that FWH functions deal with the UnixTime in millliseconds whereas MySql functions deal with Seconds.

Examples:

Code: Select all | Expand

? oCn:QueryResult( "SELECT UNIX_TIMESTAMP( ? )",  { HB_DateTime() } )
? oCn:QueryResult( "SELECT FROM_UNIXTIME( ? , '%d-%m-%Y %H:%i:%S')", { nUnixTimeInSeconds} )
Also while dealing with TimeStamps in MySql, we need to keep in mind automatic TimeZone conversions from UTC.
Regards

G. N. Rao.
Hyderabad, India
User avatar
vilian
Posts: 984
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by vilian »

Thank you Mr Rao,

If I try with your function, I'm having this result: 168486480000 for 2023-05-23T18:00:00

Code: Select all | Expand

SET DATE BRIT
myDate := Hb_Dtot(Ctod("23/05/2023"),"18:00:00")

? FW_DateToUnix( MyDate ) //168486480000
 
But in https://www.timestamp-converter.com/, I'm having a different result(as you can view bellow). Do you know why ?

Image
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by nageswaragunupudi »

Both are same.
In that website there is an implicit conversion from your time zone to UTC.
Try this code:

Code: Select all | Expand

? FW_DateToUnix( UTC_TIMESTAMP() )
Note: UTC_TIMESTAMP() is an FWH function
Regards

G. N. Rao.
Hyderabad, India
User avatar
vilian
Posts: 984
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to convert a Date to an Unix TimeStamp?

Post by vilian »

Thank you, now it's working perfectly :)
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Post Reply