Custom format DateTime to Timestamp - php

I have made many searches for a simple function to convert custom DateTime to Timestamp, But I didn't get the exact solution:
The DateTime str is:
$dt = "14-05-2015 18:21";
(Day-Month-Year Hour:Minute) (not the standard DateTime format)
Is there a simple function to convert this string to timestamp?
Thanks.

can't be simpler than:
$dt = "14-05-2015 18:21";
echo strtotime ($dt); //1431627660

If format will always be the same, you can do it like this.
$date = DateTime::createFromFormat('d-m-Y H:i', $dt);
$timestamp = $date->format('U');

Related

convert "DD/MM/YYYY h:m" to timestamp

I need to convert DD/MM/YYYY h:m eg 21/12/2015 17:14 this date to timestamp format using php and store it in mysql.
I have tried strtotime() and some other functions but it's not working. Can any one help me on this. I'm using datepicker to generate the value for date time
Use following code to convert "DD/MM/YYYY h:m" into timestamp
$dateString = "21/12/2015 17:14";
$timestamp = strtotime(str_replace("/", "-", $dateString));
you can use DateTime class:
$dateStr = '21/12/2015 17:14';
echo $timestamp = \DateTime::createFromFormat('d/m/Y H:i', $dateStr)->getTimestamp();

PHP date year format

I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));

PHP - convert date from non standard format

I am parsing a feed which returns the date in form:
20150129180000
which should be [yyyy][mm][dd][hours][minutes][seconds]
I tried formatting this with the php date() function:
date('Y-m-d H:i:s', $event['start-dt'])
and got this:
640502-03-05 12:33:20
which is obviously not right. Is there a smooth way of converting this, or do I simply need to splice the string manually?
Doc
$date = DateTime::createFromFormat('YmdHis', '20150129180000');
echo $date->format('Y-m-d H:i:s');
You can use preg_replace to format the string
$date_str = preg_replace("/^(\d){4}(\d){2}(\d){2}(\d){2}(\d){2}(\d){2}/","$1-$2-$3 $4:$5:$6",$event["start-dt"]);
date('Y-m-d H:i:s', strtotime($date_str));
Or use php's DateTime object
$date = new DateTime($date_str);
Or better yet, use the DataTime createFromFormat method
$format = "YmdGis";
$dateObj = DateTime::createFromFormat ( $format, $event['start-dt'] );
Then you can use getTimestamp to retrieve the timestamp for calculations, if you need it.
The format is somewhat confusing compared to date until you are used to it, but this corresponds to your date string:
$date = DateTime::createFromFormat('YmdGis', '20150129180000');
echo $date->format('Y-m-d H:i:s');

How to turn a string like "16/Sep/2014 08:34" to unix timestamp in PHP

Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php

Formatting date gives 12/31/1969

I'm trying to format a date in the form mm-dd-yyyy to the form yyyy-mm-dd, however, when I try formatting it, it comes out as 1969-12-31.
Here's my code:
$custom_date = "10-13-2013";
$formatted_date = date("Y-m-d", strtotime($custom_date));
What's wrong?
$custom_date = "10-13-2013";
$formatted_date = DateTime::createFromFormat("m-d-Y", $custom_date)->format("Y-m-d");
mm-dd-yyyy is not a format that is recognised by strtotime. That's because it wouldn't reliably be able to handle dates like 03-04-2013, it is the fourth of March or the third of April?
You need to parse it manually, or use the DateTime class.
list($m,$d,$y) = explode("-",$_GET['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$formatted_date = date("Y-m-d",$timestamp);

Categories