0

I'm learning PHP and I've been trying to get a two digit representation of a year from the array returned by the date_parse_from_format() function:

date_default_timezone_set('UTC');
$now = date('d.m.Y G:i:s');
$dateArray = date_parse_from_format("j.n.y H:iP", $now);
echo $dateArray['year'];

When I run this code, it prints 2020 on the browser instead of 14 as I expect. Please what am I missing in this code and how do I get the two digit year representation?

3 Answers 3

2

Try this

date_default_timezone_set('UTC');
$now = date('d.m.y G:i:s');
$dateArray = date_parse_from_format("j.n.Y H:iP", $now);
echo $dateArray['year'];
2
  • Thanks, Anand, this works even though I find this funny since The PHP Manual states that the Y format in dates returns dates with 4 digits. Dec 17, 2014 at 4:30
  • @che-azeh: uppercase yes, there is also lowercase y.
    – Glavić
    Dec 17, 2014 at 6:52
0

Try This

date_default_timezone_set('UTC');
$now = date('d.m.Y G:i:s');
$n = date ('j.n.y H:iP', strtotime($now)); //Convert to format you want
$dateArray = date_parse_from_format("j.n.y H:iP", $n);
echo '<pre>';
print_r($dateArray);
echo '</pre>';`
1
  • Thanks for the response, Rana, but it still returns the year in 4 digits. Dec 17, 2014 at 4:33
0

edit:

<?php
date_default_timezone_set('UTC');
$now = date('d.m.y G:i:s');
$dateArray = date_parse_from_format("j.n.Y H:iP", $now);
echo $dateArray['year'];
?>

date_parse_from_format() is available only in PHP 5.3, Check this question as well,

PHP date_parse_from_format( ) alternative in PHP 5.2

1
  • Still returns 4 digits, Surabhil. Dec 17, 2014 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.