dcsimg
Results 1 to 16 of 16

Thread: Read Date and Time from a .php webpage

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    57

    Read Date and Time from a .php webpage

    Hello,

    Somebody help me please.

    I have an application in which I need to display the right date and time, regardless the system time. The computer could be with the wrong date and time, which I do not want in the application.

    So, I have this webpage: http://pcdsh01.on.br/HoraLegalBrasileira.php

    I need my application to read the "Hora Oficial de Brasília" (the time displayed in the top center of the page).


    How can I achied this?

    thank you

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,637

    Re: Read Date and Time from a .php webpage

    There isn't really such thing as "a PHP web page". The URL is to a PHP script but, like all such web technologies, that script generates HTML and that is what gets sent to the browser and displayed. That page is HTML like any other so you can parse it like any other. You should do some research on parsing HTML using the HTML Agility Pack, which is a .NET tool built for the purpose that you can incorporate into your own application.

    That said, I would think that contacting a time server directly would be a far better idea than scraping a web page. Screen-scraping, as extracting data from a web page is known, is notoriously flaky because even a single character changing in the page could break your code. Contacting a server that uses a long-standing and well known protocol like a time server is likely to never break.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Read Date and Time from a .php webpage

    Yeah, this is really a job for time servers. There's a protocol called NTP and plenty of public servers to work off of.

    I did a NuGet search for NTP and found a package called "Yort NTP Client". This is all it took to get the time with it:
    Code:
    Imports Yort.Ntp
    
    Module Module1
    
        Sub Main()
            Dim ntpClient As New NtpClient()
            Dim result = ntpClient.RequestTimeAsync().Result
    
            Console.WriteLine("The current local time is: {0}", result.ToLocalTime())
        End Sub
    
    End Module
    Obviously you'd want to actually use Async/Await in a real project but this seemed simple enough.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    57

    Re: Read Date and Time from a .php webpage

    Thank you

    I found a NuGet called "NTP DateTime" .. is this a specific package for this?

    'In the package I found this code:

    Code:
    'To get current DateTime using NTP protocol:
    
    Imports System
    Imports Tarczynski.NtpDateTime
    
    Namespace NtpDateTime.ConsoleClient
        
        Class Program
            
            Private Shared Sub Main(ByVal args() As String)
                Dim ntpDateTime = DateTime.Now.FromNtp
                Console.WriteLine(ntpDateTime)
                Console.ReadLine
            End Sub
        End Class
    End Namespace
    But I am not sure how to call this in the code. I want to display the date and time in a label.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,637

    Re: Read Date and Time from a .php webpage

    That code is displaying the date and time at the console. I think that you can probably work out how to display the same thing in a Label instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,949

    Re: Read Date and Time from a .php webpage

    It's sample code... so you wouldn't call it. It's a sample on how you would use the NtpDateTime class.... you create a new instance of the class, and then display the results in a label.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,637

    Re: Read Date and Time from a .php webpage

    Quote Originally Posted by techgnome View Post
    It's sample code... so you wouldn't call it. It's a sample on how you would use the NtpDateTime class.... you create a new instance of the class, and then display the results in a label.

    -tg
    You actually don't create a new instance of the class. It's slightly odd in that it FromNtp is an extension method on the DateTime type. As in the sample code, you get/create a DateTime value and call FromNtp on it to get the current DateTime value from the server. That value is just a DateTime like any other, so you can do with it whatever you can with any other DateTime. Note that, in the example, the value of DateTime.Now is bever actually used but that method is overloaded and allows you to specify that the value it is called on is returned if the server can't be reached.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    57

    Re: Read Date and Time from a .php webpage

    Hello

    I made this: (inside my form class)

    Code:
    Public Shared Function HoraCerta() 'ByVal args() As String)
                Dim ntpDateTime = DateTime.Now.FromNtp
                'Console.WriteLine(ntpDateTime)
                'Console.ReadLine()
                Return ntpDateTime
            End Function
    In the FormLoad I call:
    Code:
    lbl_data.Text = HoraCerta()
    But the VB seems to not recognize the "FromNTP" What could be?

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,637

    Re: Read Date and Time from a .php webpage

    It could be that you didn't read the sample code properly. The namespace imports are there for a reason. Your project will almost certainly be importing the System namespace already but that FromNtp method is a member of a module that is a member of the Tarczynski.NtpDateTime namespace, so you cannot call that method without importing that namespace.

    If you don't understand what importing a namespace does, I suggest that you follow the Blog link in my signature below and check out my post on Assemblies & Namespaces.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    57

    Re: Read Date and Time from a .php webpage

    Quote Originally Posted by jmcilhinney View Post
    It could be that you didn't read the sample code properly. The namespace imports are there for a reason. Your project will almost certainly be importing the System namespace already but that FromNtp method is a member of a module that is a member of the Tarczynski.NtpDateTime namespace, so you cannot call that method without importing that namespace.

    If you don't understand what importing a namespace does, I suggest that you follow the Blog link in my signature below and check out my post on Assemblies & Namespaces.
    I've did not understand the argument given to the function
    Code:
    Public Shared Function HoraCerta(ByVal args() As String)
    What is that?


    Even with the namespace.. the FromNtp is still underlined in red

  11. #11
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    888

    Re: Read Date and Time from a .php webpage

    Quote Originally Posted by veronica.sa View Post
    I've did not understand the argument given to the function
    Code:
    Public Shared Function HoraCerta(ByVal args() As String)
    What is that?


    Even with the namespace.. the FromNtp is still underlined in red
    Have you added the nuget package to your project?

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    57

    Re: Read Date and Time from a .php webpage

    yes.. I added the NTPDateTime.. from where I got this sample

  13. #13
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    888

    Re: Read Date and Time from a .php webpage

    Quote Originally Posted by veronica.sa View Post
    yes.. I added the NTPDateTime.. from where I got this sample
    I just created a windows forms project, installed the NtpDateTime package and updated the Form1.vb so it looked like
    Code:
    Imports Tarczynski.NtpDateTime
    
    Public Class Form1
    
    Private Sub Form1_Load(sender as Object, e as EventArgs)
       Label1.Text = Date.Now.FromNtp()
    End Sub
    I added a label to the form and ran it and it worked just fine.

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    57

    Re: Read Date and Time from a .php webpage

    I unnintalled the nugetpackges and reintalled just NtpDateTime. Did like you and it is getting the loca date and time, not from the server.

  15. #15
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    888

    Re: Read Date and Time from a .php webpage

    How much of a difference is there between the local time and the server time? It might be a problem connecting to the server as the default behaviour seems to be to use the local time if there is an exception accessing the ntp server. I only know this because there is a parameter to the FromNtp method called computerTimeOnException - try setting that to false and see if there is an error raised.

    Then again is there a reason why the local time isn't going to be suitable? If the local machine is getting it's time set via NTP or similar it should also be correct.

  16. #16
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Read Date and Time from a .php webpage

    Quote Originally Posted by PlausiblyDamp View Post
    How much of a difference is there between the local time and the server time? It might be a problem connecting to the server as the default behaviour seems to be to use the local time if there is an exception accessing the ntp server. I only know this because there is a parameter to the FromNtp method called computerTimeOnException - try setting that to false and see if there is an error raised.

    Then again is there a reason why the local time isn't going to be suitable? If the local machine is getting it's time set via NTP or similar it should also be correct.
    Some language in the OP implies the system time isn't trustworthy, I'm assuming this is for some kind of license expiration check or something.

    (In which case it'd be better to perform the check on a remote server anyway, as a clever user could intercept and manipulate NTP traffic via a proxy.)

    The only advice I can give is I made a post and named the package I used, and it worked for me. The discussion follows a different package so if it's not working, well, it's not the code I suggested.

    Personally I don't like APIs that use DateTime instead of DateTimeOffset. Be very careful to check DateTimeKind when you get a result back, that should tell you if you're dealing with UTC or local in a sane API. With a DateTimeOffset, it's more clear which you're dealing with. But practically no one uses DateTimeOffset so it's a bit rare.
    Last edited by Sitten Spynne; Dec 6th, 2017 at 11:54 AM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width