Correct way of converting DateTime string in timestamp [duplicate] - php

This question already has answers here:
datetime to timestamp [duplicate]
(2 answers)
Closed 8 years ago.
Which is correct way of converting datetime string in format like '2014-12-06 07:45' in timestamp ?
I make like
date( "Y-m-d H:i", '2014-12-06 07:45' );
but time is zero...

Flexible format way:
$date = \DateTime::createFromFormat('Y-m-d H:i', '2014-12-06 07:45');
echo $date->getTimestamp();

This should work for you:
echo strtotime("2014-12-06 07:45");
Output:
1417848300

Related

PHP DateTime Object convert from format A to format B [duplicate]

This question already has answers here:
Converting to date in PHP from yyyymmdd format
(5 answers)
Closed 6 years ago.
I have the string '19720505' (year + month with leading zero + day with leading zero).
How to convert it to the format Y-m-d 00:00:00 using the PHP DateTime class?
First convert your yyyymmdd to a DateTime object:
$inputString = "19720505";
$date = DateTime::createFromFormat("Ymd", $inputString);
Then convert your DateTime object to string in desired format:
$outputString = $date->format('Y-m-d H:i:s');
All this operations are considered basic and very simple. You should read PHP Documentation before asking something so basic here.
http://php.net/manual/en/class.datetime.php
Using DateTime:
<?php
$date = new DateTime( '19720505' );
echo $date->format( 'Y-m-d H:i:s' );
http://php.net/manual/en/class.datetime.php

Convert database timestamp to date [duplicate]

This question already has answers here:
Formatting an SQL timestamp with PHP
(6 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a database timestamp with this format 2016-06-01 11:46:00 and I would like to convert and echo it to this format Wed. 01-06-2016.I tried a lot but no desirable results..Any ideas?
Thanks a lot, it worked!
If I want the difference between today and the past date in days or weeks I think I use date_diff.How exactly?
Simply use date and strtotime:
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016
Updates:
$grk = array("Tet"); // complete the rest of the array
$eng = array("Wed"); // complete the rest of the array
$time = '2016-06-01 11:46:00';
$date = date("D. d-m-Y", strtotime($time));
echo str_replace($eng, $grk, $date);
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016

Formatting date from dd-mm-yyyy to yyyy-mm-dd in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).
How do i format the string and save it in the database ?
$new_format = date("Y-m-d", strtotime('04-28-2012'));
or
$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');
or in PHP 5.5+
$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
Try
$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo $date->format('Y-m-d H:i:s') , "\n";
Output
2012-02-15 23:54:52

Convert yyyy/mm/dd-hh-mm-ss.00 to Y-m-d H:i:s in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I'm developing a system that recibe a date format like: yyyy/mm/dd-hh-mm-ss.00 and I need convert it to Y-m-d H:i:s.
What I done was:
$newDate = date("Y-m-d H:i:s", strtotime($values[2]));
where $values[2] it is the date in that format, for instace: 2014/01/01-13-18-23.00. But the result is: 1970-01-01 01:00:00
Can you help me?
Thanks
You can try with strptime like
$format = "%Y-%m-%d %H:%i:%s";
echo strptime($values[2], $format);
Try using DateTime
$date = DateTime::createFromFormat('Y/m/d-H-i-s.00', '2014/01/01-13-18-23.00');
echo $date->format('Y-m-d H:i:s');
See demo here
You should be able to do something like this:
$date = DateTime::createFromFormat("Y/M/d-H-i-s.00", $values[2]);
echo $date->format("Y-m-d H:i:s");
The dateTime format for strtotime() function are predefined and you have to follow the rules to get desired result.
Mixed date time formats are available here PHP: Compound Formats - Manual

converting a SQL server date time to mysql date time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert DateTime to VarChar
Example
How do you convert a SQL datetime (for example 08/14/2012 1:05:18 PM ) to the mysql way ( that is Y-m-d H:i:s )?
Depends on where you are wanting to convert it.
To convert in your SQL directly:
convert(varchar(23), getdate(), 121)
PHP using the date and strtotime functions:
date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
Change the format of the date in PHP like so:
<?
$date = date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
echo $date;
?>
$var = "08/14/2012 1:05:18 PM";
echo date('Y-m-d H:i:s', strtotime($var));

Categories