Datetime value to string in PHP

This one escapes me everytime I need which results to much googling. The function is to convert SQL datetime value to human-readable format.


/* 
*   Converts the likes of '2010-08-01 14:57:15' to 'August 1, 2010, 2:57 pm' 
*/  
$datetime_value ="2010-08-01 14:57:15";
echo date(  "F j, Y, g:i a", strtotime( $datetime_value ) );  

Here’s another example which you will see on some of my templates.

<?php echo date('Y-m-d', strtotime('next month')); ?>

For more formats, go head to PHP Doc.

Reference:

http://php.net/manual/en/function.date.php

Incoming search terms:

  • php date to string
  • php datetime tostring
  • date to string php
  • php datetime to string
  • datetime tostring php
  • datetime to string php
  • php convert date to string php
  • php convert date tostring
  • php date time value
  • convert date to string in php

Trim String in C++

Funny how a trivial task like trimming trailing spaces in C++ gets a bit harder if you insist on using STL (standard template library). But the good thing is, the internet is there and Google is so good at finding the right answer if you ask the right question. And after reading some samples, I found the solution.

Here’s the actual code that I used.
inline string trim_right (const string & s)
{
size_t found;
string whitespaces (" \t\n\r");

string str (s);
found=str.find_last_not_of(whitespaces);
if (found!=string::npos){
str.erase(found+1);
}
else{
str.clear();
}
return str;
}

here’s a sample function call

string str1="lorem ipsum dolor ";
string str2=" ";
str2=trim_right(str1);
cout << "->|" << str2.c_str() << "|<-";

Here's the shorter version of the code.

str=str.find_last_not_of(" \t\n\r");

Actually, there could be a ton of other implementation but that's what I'm using. If you know a better way, let me know.

Incoming search terms:

  • trim c
  • c trim
  • trim string c
  • c trim string
  • trim string in c
  • trim in c string
  • trim string in c linux
  • trim strings in c
  • trimming in c string
  • TrimRight C

String Replace in C++

As I’m just a C++ poser, I don’t know a lot of things programming in this language especially if I have to deal with STL (Standard Template Library). As some of you know, when you’re not using third party libraries in C++, programming gets a little harder (note : my harder is harder than your harder).

So I searched the web and found this solution. Unfortunately, I forgot where I got this. Anyway, the original program doesn’t work.

#include
#include

using namespace std;

int main()
{
string str( "The Horse and the Yokaba" );
string searchString( "Yokaba" );
string replaceString( "Kabayo" );

string::size_type pos = 0;
while ( (pos = str.find(searchString, pos)) != string::npos ) {
str.replace( pos, searchString.size(), replaceString );
pos++;
}
cout << str << endl;
return 0;
}

When I'm using the MString library, it was a breeze. There's a ton of methods that makes things easy but when you need to make your program faster and leaner, you wouldn't want the extra baggage that comes with a third party library. MString is good but it's just brings with it too much things I don't need on my program.

If I have time(which most of the time I spend sleeping), I'll update the post to include a line by line explanation. It's not exactly for you but for me. It's a note to myself. :D

Ciao!

Incoming search terms:

  • string replace in c
  • string replace c
  • StringReplace c