jlh3590 Posted December 3, 2008 Share Posted December 3, 2008 Can someone help me with my PHP code? I'm trying to take seconds from ths getdate() function and convert them to astreicks. For example, 23:15:06 means ******. I tried using the preg_replace with count parameter. Anyway, any help would be appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/ Share on other sites More sharing options...
Maq Posted December 3, 2008 Share Posted December 3, 2008 There's probably an easier way but... $d = getdate(); $len = strlen($d); $n = ""; for($x=0; $x { $n .= "*"; } Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705199 Share on other sites More sharing options...
jlh3590 Posted December 3, 2008 Author Share Posted December 3, 2008 I tried it and no dice... I tried putting echo n$ in the for loop and it still didn't work. Any clues? Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705265 Share on other sites More sharing options...
Maq Posted December 3, 2008 Share Posted December 3, 2008 Post your entire script. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705273 Share on other sites More sharing options...
jlh3590 Posted December 3, 2008 Author Share Posted December 3, 2008 <html> <head> <title>PHP Assignment</title> </head> <body> <p><b>Part 4</b></p> <font color="red"> <?php print date("D F d Y, H:i:s", time()); ?> </font> <br></br> <?php $d = getdate(); $len = strlen($d); $n = ""; for($x=0; $x<$len; $x++) { $n .= "*"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705279 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 Or you could do this: $d = getdate(); $len = strlen($d); echo str_pad("", $len, "*"); I would think that would work. But if you know the amount of characters you could just do: echo str_pad("", 8, "*"); If it is always 24 hour time that should be true. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705285 Share on other sites More sharing options...
jlh3590 Posted December 3, 2008 Author Share Posted December 3, 2008 Will, the problem is that I need the characters to change with the seconds everytime I reload the PHP code. Make sense? This sounds so simple to do. I missing something... Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705301 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 Will, the problem is that I need the characters to change with the seconds everytime I reload the PHP code. Make sense? This sounds so simple to do. I missing something... Thanks... No not really. You mean you want to change from ******* to --------- to ^^^^^^^^^ to &&&&&&&& ? Provide an example of what you want written out like output should look like: ****** Then the next page load it should look like: &&&&&&&& And maybe that will clear up what you want. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705320 Share on other sites More sharing options...
jlh3590 Posted December 3, 2008 Author Share Posted December 3, 2008 Part 4 Wed December 03 2008, 15:53:32 ***** <<There should be 32 astericks to correspond with 32 seconds.>> Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705325 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 Part 4 Wed December 03 2008, 15:53:32 ***** <<There should be 32 astericks to correspond with 32 seconds.>> Ahhh now that is much easier to do: $cnt = date("s", getdate()); echo str_pad("", $cnt, "*"); That should work as long as getdate() returns a valid date that can be used by the date function. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705342 Share on other sites More sharing options...
Maq Posted December 3, 2008 Share Posted December 3, 2008 One problem. The date() function returns the seconds but with the leading zeros. So, in some cases, you're going to have extra characters. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705391 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 One problem. The date() function returns the seconds but with the leading zeros. So, in some cases, you're going to have extra characters. Should be an easy fix. $cnt = date("s", getdate()); echo str_pad("", intval($cnt), "*"); intval should solve that issue. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705401 Share on other sites More sharing options...
jlh3590 Posted December 3, 2008 Author Share Posted December 3, 2008 Here's my code again: <html> <head> <title>PHP Assignment</title> </head> <body> <p><b>Part 4</b></p> <font color="red"> <?php print date("D F d Y, H:i:s", time()); ?> </font> <br></br> <?php $cnt = date("s", getdate()); echo str_pad("", intval($cnt), "*"); ?> </body> </html> This is the output: Part 4 Wed December 03 2008, 16:56:25 * Note: I'm still not getting the correct amount of astericks with seconds. Please help!!! Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705419 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 Alright, I do not think getdate is what you want. I would go this route. Also please use [ code ] tags to make code look presentable. <html> <head> <title>PHP Assignment</title> </head> <body> <p><b>Part 4</b></p> <font color="red"> <?php $now = time(); print date("D F d Y, H:i:s", $now); ?> </font> <br></br> <?php $cnt = date("s", $now); echo str_pad("", intval($cnt), "*"); ?> </body> </html> Assigned the $now to the current time and just using that to reference everything. getdate returns an array of values and you really do not need that. Hopefully the above works. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705429 Share on other sites More sharing options...
jlh3590 Posted December 3, 2008 Author Share Posted December 3, 2008 Right, I tried the time() function and it worked perfectly. I suppose getdate() returns to many keys and values which causes more confusion. Thanks for the input. I still trying to learn this stuff. Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705445 Share on other sites More sharing options...
DarkWater Posted December 3, 2008 Share Posted December 3, 2008 If you want to use the current time, date doesn't require 2 parameters: For example: date("s", time()); is the same as: date("s"); Quote Link to comment https://forums.phpfreaks.com/topic/135395-getdate-function/#findComment-705470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.