Using PHP to change the format of a MySQL date - php

I have dates stored in a MySQL database like so: 2012-02-10
When i output them using PHP, is there a function I can use that will output it like so 10/02/2012
Ive tried:
$theDate = date_format($row['date'], 'd/m/Y');
echo $theDate;
but it doesnt seem to work. Any help appretiated.
PHP Version 5.3.3

You need to use date_create() before using date_format(). This is because date_format() expects a DateTime object as the first parameter.
$date = date_create($row['date']);
echo date_format($date, 'd/m/Y');
Another way to do the same thing:
$dt = new DateTime('2012-02-10');
echo $dt->format('d/m/Y');
For the PHP 5.4 users out there it can be simplified to:
echo (new DateTime('2012-02-10'))->format('d/m/Y');
edit
To comment on the alternative solutions provided, they can be simplified to:
echo date('d/m/Y', strtotime($row['date']));
Just keep in mind that they do not account for daylight savings time or timezones like DateTime does.

The old way (non-OOP) to do it,
$t = strtotime('2012-02-10');
echo date('d/m/Y', $t);
Functions to check: strtotime, date

Here's a really simple way to do it
<?php
$theDate = $row['date'];
echo date("m/d/Y", strtotime($theDate));
?>

Here are a few examples:
$input = '2012-02-10';
// datetime (object oriented style)
$dt = new DateTime($input);
echo $dt->format('d/m/Y') . "\n";
// datetime (procedural style)
$dt = date_create($input);
echo date_format($dt, 'd/m/Y') . "\n";
// strtotime
$m = strtotime($input);
echo date('d/m/Y', $m) . "\n";
// substr
echo substr($input,8,2) . "/" . substr($input,5,2) . "/" . substr($input,0,4) . "\n";
// explode
$m = explode('-', $input);
echo "$m[2]/$m[1]/$m[0]" . "\n";
// preg_match
preg_match('~^(\d+)-(\d+)-(\d+)$~', $input, $m);
echo "$m[3]/$m[2]/$m[1]" . "\n";
// sscanf
$m = sscanf($input, '%d-%d-%d');
echo "$m[2]/$m[1]/$m[0]" . "\n";
p.s. did I miss any? ;)

Related

create altered time record based on existing value

How do you take an existing 'date and time' value and convert it to the same date but a specified time?
For example, $time="2017-09-01 13:18:00" -> how to do you convert to "2017-09-01 23:59:59"? It must keep the date but change the time to 23:59:59.
You can do it like this by explode and simple concatenation
<?php
$time="2017-09-01 13:18:00";
$date = explode(" ", $time)[0];
echo $date." 23:59:59";
?>
Live demo : https://eval.in/853822
Update
I think you need this
<?php
$date = new DateTime('2017-09-01 13:18:00');
$date->setTime(23, 59,59);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Live demo : https://eval.in/853857
What about this?
$date = new DateTime('2017-09-01 13:18:00');
$date->add(new DateInterval('PT10H30S'));

Echo date/month from database and echo current year

I would like my output to be - 05/12/2016 <--- year depends on the current year.
$date='05/12/2014';
$current_year='date("Y")';
I've tried combine like below but clearly its wrong. Please help me, how do I combine these two to get my desire output?
$date =(date("d/m", strtotime($date'])) / date("Y"));
$date='05/12/2014';
echo date("d/m", strtotime($date)) ."/". date("Y");
You should try something like this. Concate your / and remove ] .You need to format your database date.It should work.Try:
$databaseDate = date("d/m/Y", strtotime("31/12/2014")); //you need to format your databse Date
$mergeDate = date("d/m", strtotime($databaseTime)) . '/' . date("Y");
echo $mergeDate;
LIVE DEMO
Your approach can work. Just adapt it a bit:
date("d/m", strtotime($date)) . '/' . date("Y");
EDIT:
As pointed out by others, with this type of date format (d/m/Y) you might have issues when date is '25/12/2014'. To be sure your code works in any case use date_parse_from_format to control your date format:
$date = '25/12/2014';
$date_array = date_parse_from_format('d/m/Y',$date);
echo $date_array['day'] . '/' . $date_array['month'] . '/' . date("Y");

PHP - strtotime() return 1970

The following code snippet:
echo date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"));
returns me:
01.01.1970-01:00:00
What's the right way to convert "01.01.2000-11:12:32" to time/date object, for comparing it with the current timestamp?
e.g.
if (date("d.m.Y-H:i:s") > date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"))) {
echo "future";
} else {
echo "past";
}
This is due to localisation. Try giving a different format, as the format matters a lot:
echo date("d.m.Y-H:i:s", strtotime("01/01/2000 11:12:32"));
echo date("d.m.Y-H:i:s", strtotime("01-01-2000 11:12:32"));
You should not have . for date and month separator.
You cannot separate date and time using -.
If you are getting the input from another source, try using str_replace:
echo date("d.m.Y-H:i:s", strtotime(str_replace(array(".", "-"), array("/", " "), "01.01.2000-11:12:32")));
Output: http://ideone.com/d19ATK
Try to replace . with -:
echo date("d.m.Y-H:i:s", strtotime(str_replace('.', '-', "01.01.2000 11:12:32")));
Also remove - between the date and time.
You can use DateTime::createFromFormat
$date = DateTime::createFromFormat('d.m.Y-H:i:s', '01.01.2000-11:12:32');
$now = new DateTime();
if ($now > $date) {
echo "future";
} else {
echo "past";
}

How to convert DateTime in UTC format?

I have time in this format and I want to convert this into UTC timezone.
: "2015-03-17T07:46:52+0100"
: "yyyy-MM-dd'T'HH:mm:ssZ"
I am using Codeigniter framework.
Pretty straightforward using DateTime objects
$string = "2015-03-17T07:46:52+0100";
$dt = new DateTime($string);
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s');
Try the getTimezone and setTimezone, see the example
(But this does use a Class)
UPDATE:
Without any classes you could try something like this:
$the_date = strtotime("2015-03-17T07:46:52+0100");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");

Add days & dispaly output in string

I need to add days to given string date and display calculated date in string
This is what I have tried, but I could not make it work.
$date = date_create('1-Feb-2012');
$newDate = date_modify($date, '+2 day');
echo 'Your date is' . $newDate . '.';
This gives an error
Object of class DateTime could not be converted to string
You need to tell the DateTime object how to format its output using DateTime::format. So, for example:
$date = new DateTime('1-Feb-2012');
$date->modify('+2 day');
echo 'Your date is' . $newDate->format('Y-m-d H:i:s') . '.';
Also note that modify directly modifies the DateTime - it doesn't just return a new one, as the documentation might lead you to believe - so I've removed the second variable. I've taken the liberty of changing the objects to the object-oriented form as well, which you should be using :)
Here's a working demo.
Use DateTime::format function.
usage:
echo 'Your date is' . $newdate->format('Y-m-d H:i:s') . '.';
or
echo 'Your date is' . date_format($newdate, 'Y-m-d H:i:s') . '.';

Categories