开发者

PHP add colons to a string

开发者 https://www.devze.com 2023-03-13 08:54 出处:网络
Hello I have this string that will generate the time that I then insert in the db $time=mktime(date(\'G\'),date(\'i\'),date(\'s\'));

Hello I have this string that will generate the time that I then insert in the db

$time=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('Gis',$time);
开发者_开发问答

Now I need to readd colons to this string: :

I need it to be generated and inserted without colons, but shown in another place with colons reinserted, so that it will look like this:

13:24:09

Instead of:

132409

The same is for $todaydate = date('Ymd');

That should become then 2011-06-16

How can I do that?

Counting the words is not good, since we can have more or less types depending by the actual time and date.

Please advise

Thank you!

FOR WHO DOES NOT UNDERSTAND: this values are taken from the DB so I cannot use : date('Y-m-d'); in a values taken from the db........


The same is for $todaydate = date('Ymd');

That should become then 2011-06-16

For this one, try :

date('Y-m-d');

Similar for your Other part.

Edit :: For Date :

$time = "time from database"; $unixtimestamp = strtotime ( $time) ; $date = date('Y-m-d', $unixtimestamp);

For time :

$time = strtotime("time from database"); 
$yourtime = date("H:i:s", $time);


The strings you supply for formatting can contain anything you'd like, so putting the dashes or semicolons there is no problem. You can even include other text, as long as any letters used in the date code are escaped with a backslash.

The entire

$time=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('Gis',$time);

could be rewritten, too. You're maing a timestamp from the current time, then giving it to date(). date uses the current time by default, so there's no need to do that to show the current time.

Here's one way:

$NowisTime=date('G:i:s');


If you are doing exactly what you say you are doing, you can count the characters starting at the end and add the characters (: and -) at the right place:

Both date('is') and date('Ymd') produce a fixed format using leading zeros so the length is always the same. You only have to compensate for the date('G') part.

So really all you have to do is chop off 2 characters from the end of the string twice and what remains is the year or the hour.


Well if you use date('his') instead and cast it as a string (so PHP doesn't interpret it as an integer and remove the possible leading zero), you can add colons by splitting the string every two numbers and then imploding it with colons.

$d = (string)date( 'his' );
echo (int)$d; //Format without colons
echo implode( ':', array_map('intval', str_split( $d, 2 ) ) );

For the second part, do the same except split the string by 4 characters and then split the second split by 2.

$d = date('Ymd');
echo $d; //Format without dashes
list($year, $second) = str_split( $d, 4 );
$parts = str_split( $second );
array_unshift( $parts, $year );
echo implode( '-', $parts );

In both situations however it would just be easier to start out with the formatted strings (with the colons and dashes) and remove them for the db.

$d = date('G:i:s');
echo $d; //With colons
echo str_replace( ':', '', $d );

$d = date('Y-m-d');
echo $d; //With dashes
echo str_replace( '-', '', $d );


When you are generating the value to store in your database, use a Unix timestamp:

$timestamp = time();
// store $timestamp in your database

That way you don't have to worry about how it looks or parsing it at all. Then, when you're ready to display it to your user, you can use:

$formattedTime = date('G:i:s', $timestamp);

which will display it in the colonated (is that even a word?) format for your users.


Couldn't you do something like :

$new = "";
for ($i = 2; $i <= strlen($NowisTime); $i+2) {
    $new .= $NowisTime[$i-2] . $NowisTime[$i-1] . ":";
}


$NowisTime=date('G:i:s',$time);
$todaydate = date('Y-m-d');
0

精彩评论

暂无评论...
验证码 换一张
取 消