Simple way of getting the date - php

I have date in this format: 2010-01-11. I want to display JAN-11. How can i do this using php?

<?php echo date("M-y",strtotime("2010-01-11"));?>

Rudu's answer is correct, +1.
"M-y" would result to "Jan-11"
so if you need the output to be UPPERCASE, dont forget to use strtoupper().
<?php echo strtoupper(date("M-y",strtotime("2010-01-11")));?>
also bookmark this for later reference.
http://www.php.net/manual/en/function.date.php
Cheers

Related

PHP echo Date from HTML Form and format it from Y-m-d to d-m-Y error 31-12-1969

I have a text link to pass a date GET in format Y-m-d because is comming from MySQL like that.
The page receiving this GET uses the follow code to echo the date I got from previous page
the link is like this
page.php?thedate=2015-06-30
The page I need to show this date must convert this date like this
<?php echo date('d-m-Y',strtotime($_GET["thedate"]));?>
so we show 30-06-2015
But it show with this code
31-12-1969
What can the problem be?
Your code is looking OK. But few thoughts, what could go wrong:
At first: Try the following code:
<?php
var_dump($_GET['thedate']); // check what it is?
date_default_timezone_set('Europe/Warsaw'); // set default timezone to your zone / user zone
echo date('d-m-Y',strtotime('2015-06-30')); // check that date is working with plain text
?>
At first remember to always use date_default_timezone_set('Europe/Warsaw');. It can be really important for Your application. Second, enter the date in plain text mode. If this works, it mean that the date function is working ok, and there is something wrong with Your GET or after GET variable manipulation.
Second: NEVER, EVER DON'T VALIDATE _GET VARIABLES. Sorry for double negation, but maybe becouse of that, it can be more memorable ;).
Always parse the data that user enters in the URL. Always parse:
_GET data
_POST data
_REQUEST data
_SERVER data (yes, some of them can be manipulated
all other vulnerable data...
So summing up: Your code (the one You entered is OK), check the GET variable and the after get variable manipulation. And always check all the data that is not trusted.
More on validating data:
filter_var: http://php.net/manual/en/function.filter-var.php
preg_match: http://php.net/manual/en/function.preg-match.php
htmlspecialchar: http://php.net/manual/en/function.htmlspecialchars.php
According to one of the comments solution with using urldecode on _GET variable:
<?php echo date('d-m-Y',strtotime(urldecode($_GET["thedate"])));?>
"Warning! The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."
Source: http://php.net/manual/en/function.urldecode.php

string name out of vlaue of another string php

in PHP I have the following case:
<?php $anz1='3';$i='1';echo $anz.$i;?>
I don't know how to echo $anz1 with the help of $i. I must do it this way, this is just an easy case, I need it for loops where it echos every $anz
Thanks in advance
I hope I could write it understable for you, my english is not the very best.
Maybe this is what you're looking for:
$anz1='3';
$i='1';
echo ${'anz'.$i};
Output:
3

What sort of date is this in PHP

I have a date I need to replicate in PHP. I just want to know if there is some sort of shortcode for it.
2013-05-09T15:23:59.802Z
It is ISO 8601. In PHP you would make it using:
echo date('c'); // as of PHP 5
See date()
This one liner achieves what you are asking.
echo date_format(date_create("#".$unix_timestamp, timezone_open('UTC')), 'Y-m-d\TH:i:s.000\Z');
Output:
2013-05-09T15:23:59.000Z

PHP ARGB to Alpha-Hex and Alpha-Hex to ARGB

I got the following color -16777216 and I want to convert it to a alpha-hex code like this 00FFBBCC in PHP. I also want to be able to do the reverse. I really don't know where to start and my friend Google has no answer for me.
Any one can help please ?
Thank you.
What about using dechex()?
echo dechex(-16777216);
It outputs ff000000
If you want uppercase letters, simply use strtoupper():
echo strtoupper(dechex(-16777216)); //FF000000
Edit: to do the reverse, use hexdec() instead of dechex()

how to convert an int/value into a curreny/price?

im using drupal,
I have a field, where users specify a price.
Ive printed the price field in my tpl.
<?php print $node->my_form['amt'] ?>
but the price comes out like 0.050, How do I get this to display £0.50?
See money_format() or see sprintf() or the printf variant:
<?php printf('£%01.2f', $node->my_form['amt']) ?>
Output: £0.05
Demo
See money_format().
you might want to check the details here http://www.drupalcommerce.org/faq/currency-format and make sure you have a currency formatter configured/chosen.
Depending also on how you're geting your prices, you might want to see this issue http://www.drupalcommerce.org/node/633 with the solution.

Categories