Jump to content

Get time in HTML code

- - - - -

  • Please log in to reply
9 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi guys,

How to get Time in this code?

			<table class=com cellspacing=0 cellpadding=0>

			<tr><td width=31 align=right>15:30

			    <td width=36 align=right style='padding:0'>NORMAL


			</tr>


			</table>


I want get (15:30) for change time

Help me please

Thank you

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hey, you may use the date function, a reference: PHP: date - Manual
            <table class=com cellspacing=0 cellpadding=0>             
                            <tr><td width=31 align=right><?php echo date("H:i");?>
                            <td width=36 align=right style='padding:0'>NORMAL</tr>                  </table>
This will display 24 hour time with the leading zeros, i.e. 00:42 or 23:50
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
How to get time with preg in PHP?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
In that case, it would look something like this (although it is not the best way to extract information from HTML). Note I have allowance for AM or PM in both cases:
<?php
$htmlstring = "            <table class=com cellspacing=0 cellpadding=0>
            <tr><td width=31 align=right>15:30
                <td width=36 align=right style='padding:0'>NORMAL

            </tr>

            </table>";
            
preg_match('/.*?((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)/', $htmlstring, $time);
 
print "Time from HTML is " . $time[1];

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
grisha

grisha

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
For DOM manipulation I would recommend SimpleXML or DOM. Really bad HTML by the way...

#6
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Thank you Alexander, you are best

#7
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Alexander, I need a preg_match code for get Date, for example this date: 2011-02-09

			<table class=com cellspacing=0 cellpadding=0>

			<tr><td width=31 align=right>2011-02-09

			    <td width=36 align=right style='padding:0'>NORMAL


			</tr>


			</table>


#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
If it is your wish to use regular expressions, this is an expression to match the date in YYYY-MM-DD format.

$htmlstring = '.... 2011-02-09 ....';
preg_match('/((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3}))[-:\\/.](?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[0-2]?\\d{1})|(?:[3][0,1]{1})))(?![\\d])/is', $htmlstring, $date);
print "The date: " . $date[1];

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

grisha said:

For DOM manipulation I would recommend SimpleXML or DOM. Really bad HTML by the way...
You'd probably have to use regular expressions on this HTML since it's broken. Browsers will fix the code for you using a "best guess" scenario, however when viewing the source it's raw and when viewing a td that is left open will probably get more than you asked for.

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
That is why you should generally parse the HTML through an HTML tidy before working with pure DOM trees.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users