This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert date to timestamp in PHP?
I want to transfer some date to timestamp ,the date formart is %day %month %time. Like 25 nov 07:44, I tried some method below, but the two dates are different. need a help, thanks.
<?php
$date = '25 nov 07:44';
$time = explode(" ",$date);
$minute = explode(":",$time[2]);
$timestamp = mktime(''.$minute[0].' '.$minute[1].' 00 '.$time[1].' '.$time[0].' 2011')."<br />";// mktime(%hour, %minute, %second, %month, %day, %year);
echo $timestamp; //1322202671
echo date("Y-m-j H:i:s", $timestamp); //2011-11-25 07:31:11
?>
A few problems:
You are feeding mktime a string instead of a comma separated list
$time[1] is a string and mktime needs a number for the month
You might want to give it a try with strtotime or otherwise add a translation table to go from your month strings to a number.
In recent versions of php (5.3+), you can use DateTime::createFromFormat().
Try something like
$format = 'd M H:i';
$date = DateTime::createFromFormat($format, $timestamp);
echo $date->format('Y-m-j H:i:s');
Personally, I find this class based approach a little cleaner.
PHP Docs:
DateTime::createFromFormat
date() for info on format strings
Related
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 4 years ago.
date('m/d/Y', strtotime('7-Jan-69'))
It gives output as 01/07/2069, Where
date('m/d/Y', strtotime('7-Jan-75'))
This gives output as 01/07/1975, Why is so and what is the catch?
From the docs:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)
Any date before 1970 will be understand as date after 1970
do you need something like this?
<?
// function to convert string and print
function convertString ($date)
{
// convert date and time to seconds
$sec = strtotime($date);
// convert seconds into a specific format
$date = date("Y-m-d H:i", $sec);
// append seconds to the date and time
$date = $date . ":00";
// print final date and time
echo $date;
}
// Driver code
$date = "06/12/2014 04:13 PM";
convertString($date);
?>
To fix that you can use DateTime instead of strtotime() like below,
<?php
$date = $dt = new DateTime('7-Jan-75');
echo $date->format('m/d/Y');
?>
Reason for not working in your case with strtotime:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999.
See the notes below for possible differences on 32bit systems
(possible dates might end on 2038-01-19 03:14:07).
DEMO: https://3v4l.org/d8eoK
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 9 years ago.
I am trying to separate time from the given string date & time. I simply tried with following example.
$time = '2014-01-20 10:45:45';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
I am getting correct date and time.
10:45:45
20 January 2014
But when I tried with given string, no correct result.
$time = '1899-12-30 19:30:00';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
PHP is always returning me following result.
00:00:00
01 January 1970
I am not sure whether is there any limitation on date function that is not returning 1899. Is that so?
Your date is before the unix epoch. DateTime() allows you to work around that.
$dt = new DateTime("1899-12-30 19:30:00");
echo $dt->format("d F Y");
echo $dt->format("h:i:s");
Use DateTime and DateTime::format():
$time = '1899-12-30 19:30:00';
$dt = new DateTime($time);
echo $dt->format('d F Y H:i:s');
Working example: http://3v4l.org/fM22Z
The strtotime() method is limited by the Unix epoch, which is Jan 1, 1970.
Update: As Mark comments below, your code would work all the way back to 1901 (as a negative timestamp), see here: http://3v4l.org/CSJte
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
This question already has answers here:
How to get time and date from datetime stamp in PHP?
(7 answers)
Closed 9 years ago.
i have a table in my cakephp which have a field name datetime and datatype is datetime
storing in this format
2013-06-18 00:00:00
I need to extract the date part of the value not the time ..
i extract time like this
$dateTime = $recentCall['Calllog']['dateTime'];
$time = date("H:i:s",strtotime($datetime));
now i want to extract the date part.which i dont know how can i do this .. i have done some research but nothing works out for me
This isn't exactly a CakePHP answer, but a PHP one.
$dateTime = $recentCall['Calllog']['dateTime'];
$timestamp = strtotime($dateTime);
$date = date('Y-m-d' , $timestamp);
$time = date('H:i:s' , $timestamp);
If you want to do somethink more Cakeish and use the CakePHP wrapper, should be:
$dateTime = $recentCall['Calllog']['dateTime'];
$timestamp = CakeTime::fromString($dateTime);
$date = CakeTime::format($dateTime, 'Y-m-d');
$time = CakeTime::format($dateTime, 'H:i:s');
I hope my answer is clear.
Ref: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
Use Y-m-d as first parameter in date to extract date.
$date = date("Y-m-d",strtotime($datetime));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
increment date by one month
I have the following code to sum dates:
$var1 = date("d/m/Y");
$dia = substr($var1,0,2);
$mes = substr($var1,3,2);
$a_o = substr($var1,6,4);
$date = $a_o."-".$mes."-".$dia;
$new_date = strtotime( "+3 month", strtotime($date));
$new_date = date('d/m/Y', $new_date);
echo $new_date;
That is the normal method I found on php.net but when I tried to change $var1 with another date, for example, '21/07/2020' the result is '31/01/1970', I don't know what's wrong. I tried many forms but the result is the same.
How can I fix this? Or what other forms exist to add X months to a date?
Look into the date_add() function that was added for 5.3.0.
DateTime::add -- date_add — Adds an amount of days, months, years,
hours, minutes and seconds to a DateTime object
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
There is also the date_modify() function that was added for 5.2.0.
DateTime::modify -- date_modify — Alters the timestamp
<?php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
?>
You're going about it completely wrong. date() gives you a nicely formatted STRING. If you want to do math with dates, you need to work with the native PHP date/time values, which are simple integers - unix time stamps.
You're taking that unix timestamp (the default value in date()), converting it to a string, taking apart that string, recombining the string in a different order, then converting it back to a timestamp, then converting the timestamp into a different formatted string. That's like going on a 500mile road trip so you can cross the street.
Any reason you can't simply do:
$new_date = date('d/m/Y', strtotime('+3 month'));
and eliminate everything else in your code snippet?
In your place, I'll trasform the date in timestamp, then I'll add the new date in seconds (i.e. 3 months are 60 * 60 * 24 * 90 = 7 776 000 secs.) and then transform back in date :P