Skip to content

Instantly share code, notes, and snippets.

@RodolfoSilva
Last active March 9, 2016 19:34
Show Gist options
  • Save RodolfoSilva/9207358 to your computer and use it in GitHub Desktop.
Save RodolfoSilva/9207358 to your computer and use it in GitHub Desktop.
Converter data converteData('format', 'para_formato', 'data', 'timezone')
<?php
function converteData($format, $to_format, $date, $timezone = null)
{
if (!empty($date)) {
$timezone = $timezone ? $timezone : new DateTimeZone(date_default_timezone_get());
$f_date = date_create_from_format($format, $date, $timezone);
return date_format($f_date, $to_format);
}
return false;
}
/*
* Exemplos
*/
var_dump(converteData('d m Y', 'Y-m-d', '06 02 2025')); //2025-02-06
var_dump(converteData('d-m-Y', 'm/d/Y H:i', '06-02-2014')); //02/06/2014 12:39
var_dump(converteData('Y-m-d', 'l F Y H:i', '2014-02-06')); //Thursday February 2014 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment