String to date conversion issue in PHP5.6 [duplicate] - php

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

Related

Converting an epoch timesramp to a formatted date string in php [duplicate]

This question already has answers here:
How to convert a 13 digit Unix Timestamp to Date and time?
(3 answers)
Closed 4 years ago.
I am attempting to convert an epoch timestamp value to a formatted date string in PHP and getting unexpected results:
$dateStart = 1104555600000;
$formattedDate = date('Y-m-d', $dateStart);
Expected Result: 2005-01-01
Actual Result: 1993-09-26
1104555600000 is equivalent to 2005-01-01
However, $formattedDate ends up being 1993-09-26
Actually you epoch timestamp is in millisecond, convert it to second and then convert it to date
<?php
$dateStart = 1104555600000;
echo $formattedDate = date('Y-m-d', $dateStart/1000);
Output:- https://eval.in/981417

Convert date and time format in passed date value from db [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date value from the database and its format is: YYYY-mm-dd hh:mm:ss
I want to convert it to this format: YYYY/mm/dd hh:mm
Here is my code:
<?php
$datetime = $value['last_online']; //from db
$date = date('d-m-Y', $datetime);
$time = date('Gi.s', $datetime);
echo $date.' '.$time;
?>
But it's not working and is giving me an error of:
A non well formed numeric value encountered
I want to get rid of the minus signs and the seconds so my new date and time format would be YYYY/mm/dd hh:mm. How do I do it?
Any ideas?
Use like below
$datetime = $value['last_online']; //from db
$date = date('Y/m/d h:i', strtotime($datetime));

php convert string to date [duplicate]

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

How can I add dates in PHP [duplicate]

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

php date to timestamp [duplicate]

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

Categories